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

相關文章
相關標籤/搜索