【发布时间】:2015-10-24 22:06:29
【问题描述】:
我正在接收来自服务器的 JSON 响应。
do {
let response = try NSJSONSerialization.JSONObjectWithData(data!,options:NSJSONReadinOptions.AllowFragments)
let cityDetails = reponse["cityDetails"]
if cityDetails!.isKindOfClass(NSArray) {
}
}catch {
println("Error \(error)")
}
我收到以下消息
Value of optional Type "AnyObject?" not unwrapped did you mean to use ! or ??
我稍后添加的更正是使用 double !!。如果cityDetails!!.isKindOfClass(NSArray)
下面列出了两个问题来理解这一点?
1) 即使对象已经解包过一次,为什么还要再次解包。
2) 下面的代码做同样的事情,但只需要解包一次。此外,它崩溃是因为 result 恰好是 nil。在 Objectice C 中,nil 已经是句柄并且在这种情况下将返回 false,而不是使应用程序崩溃。在那个声明中。
let testDictionary:[String:AnyObject] = ["a",NSMutableArray()]
let result = testDictionary["C"]
if result.isKindOfClass(NSArray) {
}
输入后的答案 2)为了解决第二个问题,我使用以下代码。
方法 1
if let _ = result {
if result.isKindOfClass(NSArray) {
}
}
方法 2
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let testDictionary:[String:AnyObject] = ["a":NSMutableArray()]
let result = testDictionary["C"]
print("Before details Check")
guard let details = result where details.isKindOfClass(NSArray) else {
return false
}
print("Returned Result \(details)")
print("After Details Check")
return true
}
【问题讨论】:
-
不,你应该使用 guard (guard let result = result) 解开结果,然后你就可以安全地调用它了。
-
gaurd let details are not print out后出现错误声明
-
将 result!.isKindOfClass(NSArray) 替换为 details.isKindOfClass(NSArray),这是展开后的值。但这应该可行,我想,你还有问题吗?
-
是的。调用了之前的语句,但没有打印出“详细检查之后”。另外,我尝试了以下但没有工作守卫让详细信息=结果详细信息.isKindOfClass(NSArray)。我参考了这个链接stackoverflow.com/questions/30791488/swift-2-guard-keyword。其中 gaurd 具有可变名称作为输入。
-
如果在上面添加 print("Conversion failed") 会返回 false。那个打印?如果是这种情况,在 return false 和 return true 处都为调试器设置断点,那么你可以看到想要的细节真的是如果它没有转换。
标签: ios objective-c swift