简体   繁体   中英

Is it possible to convert a JSValue into an NSNumber?

This will show the following error: 'JSValue' is not convertible to 'NSNumber'. If it's not possible to convert, how should I go about getting the JSValue and assigning it to my NSNumber variable?

    var sentences:NSNumber = getSentences.callWithArguments([])

According to the header file JSValue.h , you need to call toNumber() on your JSValue object. So it's probably:

var sentences:NSNumber = getSentences.callWithArguments([]).toNumber()

You can try:

if let sentences = getSentences.callWithArguments([]) as? NSNumber {
    // consume sentences here
}

The if let structure is probably the simplest access to it since it may not actually BE a number, If that doesn't work, you'll have to go back to JavaScriptCore and call JSValueToNumber :

let sentences = getSentences.callWithArguments([])
let nSentences = JSValueToNumber(context, sentences, nil)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM