xcode Swift 4:无法将“__NSCFNumber”类型的值转换为“NSString”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48847788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift 4: Could not cast value of type '__NSCFNumber' to 'NSString'
提问by jgravois
I have tried
我试过了
self.adc_role_id = String(res["adc_role_id"])
self.adc_role_id = "\(res["adc_role_id']"
self.adc_role_id = (\(res["adc_role_id"] as? String)!
but still get
但仍然得到
Could not cast value of type '__NSCFNumber' to 'NSString'
无法将“__NSCFNumber”类型的值转换为“NSString”
I added the dump of res[4] below
我在下面添加了 res[4] 的转储
As new as I am to Swift, I don't know anything else to try
就像我对 Swift 的新手一样,我不知道还有什么可以尝试的
回答by Mike Taverne
In Swift 4, the String initializer requires the describing:
argument label.
在 Swift 4 中,字符串初始值设定项需要describing:
参数标签。
I don't know if this will solve your problem, but your first line of code should be written:
我不知道这是否能解决您的问题,但您的第一行代码应该是这样写的:
self.adc_role_id = String(describing: res["adc_role_id"])
回答by Eric Aya
In your screenshot we can see that res["adc_role_id"]
is an NSNumber.
在您的屏幕截图中,我们可以看到这res["adc_role_id"]
是一个 NSNumber。
To transform an NSNumber to a String you should use its stringValue
property.
要将 NSNumber 转换为 String,您应该使用其stringValue
属性。
And since a dictionary gives an Optional, you should use optional binding to safely unwrap it.
并且由于字典给出了 Optional,您应该使用 optional 绑定来安全地解包它。
Example:
例子:
if let val = res["adc_role_id"] {
self.adc_role_id = val.stringValue
}
You could also, if you want, use string interpolation instead of the property:
如果需要,您也可以使用字符串插值而不是属性:
if let val = res["adc_role_id"] {
self.adc_role_id = "\(val)"
}
but I think using the property is more relevant.
但我认为使用该属性更相关。
If for some reason the compiler complains about the type of the content, cast it:
如果由于某种原因编译器抱怨内容的类型,请将其转换为:
if let val = res["adc_role_id"] as? NSNumber {
self.adc_role_id = val.stringValue
}
Note that you should notuse String(describing:)
because this initializer will try to represent the string in many ways, and some of them will give inaccurate and unexpected results (for example, if String(describing:)
resolves to use the debugDescription
property, as explained in the documentation, you may get a totally different string than the one you want).
请注意,您应不使用String(describing:)
,因为这个初始化会尽量表现在许多方面的字符串,其中一些将给予不准确和意想不到的结果(例如,如果String(describing:)
解析为使用debugDescription
属性,如文档中解释的,你可能会得到一个与您想要的字符串完全不同)。
It's also worth noting that using String(describing:)
with an optional value such as your dictionary will resolve to a wrong string: String(describing: res["adc_role_id"])
will give Optional(yourNumber)
! This is why Mike's answer is wrong. Be careful about this. My advice is to avoid using String(describing:)
altogether unless for debugging purposes.
还值得注意的是,使用String(describing:)
可选值(例如您的字典)将解析为错误的字符串:String(describing: res["adc_role_id"])
将给出Optional(yourNumber)
! 这就是为什么迈克的答案是错误的。小心这一点。我的建议是避免String(describing:)
完全使用,除非出于调试目的。
回答by vadian
The error message is clear and the dump is clear, too.
错误消息很清楚,转储也很清楚。
The value is not a String
, it's an Int(64)
wrapped in NSNumber
值不是 a String
,它是一个Int(64)
包裹在NSNumber
Optional bind the value directly to Int
(NSNumber
is implicit bridged to Int
) and use the String
initializer.
可选地将值直接绑定到Int
(NSNumber
隐式桥接到Int
)并使用String
初始值设定项。
if let roleID = res["adc_role_id"] as? Int {
self.adc_role_id = String(roleID)
}
Please conform to the naming convention that variable names are camelCasedrather than snake_cased
请遵守命名约定,即变量名是驼峰式的而不是蛇形的