我实际上是在尝试在 Xcode8.1 上使用 Swift3 解析 Json 对象。 这是我的代码:
if let objData = objJson["DATA"] as! NSDictionary? {
var msg: String = ""
if let tmp = objData.object(forKey: "Message") {
msg = tmp as! String
} else {
print("NIIILLLLL")
}
}
我收到此错误消息:Could not cast value of type 'NSNull' (0x4587b68) to 'NSString' (0x366d5f4)
at this line msg = tmp as!字符串
.
我不明白为什么会出现此错误,因为 tmp 的类型是 Any
并且它应该显示打印而不是将 tmp 转换为!字符串
谢谢你的帮助,
最佳答案
您可以在 let
中添加转换。
if let tmp = objData.object(forKey: "Message") as? String {
msg = tmp
}
关于swift - 如何检查字段类型是否为 Any?是零或 NSNull,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40687328/