简体   繁体   English

是否可以将JSValue转换为NSNumber?

[英]Is it possible to convert a JSValue into an NSNumber?

This will show the following error: 'JSValue' is not convertible to 'NSNumber'. 这将显示以下错误:'JSValue'无法转换为'NSNumber'。 If it's not possible to convert, how should I go about getting the JSValue and assigning it to my NSNumber variable? 如果无法转换,应该如何获取JSValue并将其分配给我的NSNumber变量?

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

According to the header file JSValue.h , you need to call toNumber() on your JSValue object. 根据头文件JSValue.h ,您需要在JSValue对象上调用toNumber() 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 : if let结构可能是对它的最简单的访问,因为它实际上可能不是数字,如果不起作用,则必须返回JavaScriptCore并调用JSValueToNumber

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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