昨天我更新到 El Capitan beta 2 和 Xcode 7 - beta 是强制性的。所以我将我的应用程序更新为 Swift 2,新的错误出现在 json 字符串中。这是我的代码:
让 jsonData:NSDictionary = NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers) 作为! NSDictionary
这是错误:Call can throw , but it is not marked with 'try' and the error is not handled
最佳答案
您需要将其包装在 do/catch
block 中,因为这是报告错误的首选方式,而不是使用 NSError
:
do {
let jsonData = try NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers ) as! NSDictionary
// use jsonData
} catch {
// report error
}
关于ios - Swift 2 jSON Call 可以抛出但未标有try,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31022965/