ios - Swift 2 jSON Call 可以抛出但未标有try

标签 ios json xcode7 swift2

昨天我更新到 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/

相关文章:

iphone - 使用 NSDateformatter 将 NSString 转换为 NSDate

ios - 在 Ios 的 UIWebview 中加载或查看 .doc 文件

objective-c - 手指画的效率问题

ios - 如果在Swift 2.0中让警告

ios - 按下“完成”按钮后,如何设置默认日历?

javascript - Angular ng-bind 显示“对象而不是值”

python - 如何导入自定义 json 模块而不是 python 2.7 中的默认模块?

python - Flask-ReSTLess 从 Flask-Sqlalchemy 转储 Decimal 值

ios - 调用 Super.init(编码器 : aDecoder)) 时应用程序突然崩溃

ios - XCodebuild无法在模拟器上运行测试