tôi có mã nàyCố gắng sử dụng NSJSONSerialization.JSONObjectWithData trên Swift 2
let path : String = "http://apple.com"
let lookupURL : NSURL = NSURL(string:path)!
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(lookupURL, completionHandler: {(data, reponse, error) in
let jsonResults : AnyObject
do {
jsonResults = try NSJSONSerialization.JSONObjectWithData(data!, options: [])
// success ...
} catch let error as NSError {
// failure
print("Fetch failed: \(error.localizedDescription)")
}
// do something
})
task.resume()
nhưng nó là không trên dòng let task
với lỗi:
invalid conversion from throwing function of type (__.__.__) throws to non throwing function type (NSData?, NSURLResponse?, NSError?) -> Void
gì là sai? Đây là Xcode 7 beta 4, iOS 9 và Swift 2.
chỉnh sửa:
vấn đề dường như là với những dòng
do {
jsonResults = try NSJSONSerialization.JSONObjectWithData(data!, options: [])
// success ...
} catch let error as NSError {
// failure
print("Fetch failed: \(error.localizedDescription)")
}
tôi loại bỏ những dòng này và lỗi let task
biến mất .