Call can throw, but it is not marked with 'try'

执行代码html

let user = [
            "uname": "张三",
            "tel": ["mobile": "138", "home": "010"]
        ]
let data : NSData! = NSJSONSerialization.dataWithJSONObject(user, options: nil, error: nil)

let json : AnyObject! = NSJSONSerialization
            .JSONObjectWithData(data, options:NSJSONReadingOptions.AllowFragments, error:nil)

报错内容ios

Call can throw, but it is not marked with 'try' and the error is not handled Extra argument ‘error’ in calljson

运行环境: Xcode Version 7.1 (7B91b)swift

修改后的代码.net

let data : NSData! = try? NSJSONSerialization.dataWithJSONObject(user, options: [])
let json : AnyObject! = try? NSJSONSerialization
            .JSONObjectWithData(data, options:NSJSONReadingOptions.AllowFragments)

参考:code

http://www.jianshu.com/p/eb718439551f
http://www.hangge.com/blog/cache/detail_647.html
http://www.cocoachina.com/swift/20150707/12355.html
http://blog.csdn.net/ios_of_swift/article/details/47280483htm

[END]blog

相关文章
相关标签/搜索