0
我正在測試Ray Wenderlich的這個代碼以從URL下載數據。我得到它的工作,但我更新到Xcode 8.3,現在突然它不再工作。我得到的建議回來說,我的問題已經在下面的鏈接SWIFT: Error EXC_BAD_INSTRUCTION已解決,但是,添加試試!選項或按建議添加do-catch,這不會導致錯誤消失。這裏是再次驗證碼:當在Xcode 8.3中使用loadDataFromURL時發生錯誤
class func loadDataFromURL(_ url: URL, completion:@escaping (_ data: Data?, _ error: NSError?) -> Void) {
let session = URLSession.shared
// Use NSURLSession to get data from an NSURL
let loadDataTask = try! session.dataTask(with: url, completionHandler: { (data: Data?, response: URLResponse?, error: NSError?) -> Void in
if let responseError = error {
completion(nil, responseError)
print("something went wrong 1")
//activityIndicator.stopAnimating()
} else if let httpResponse = response as? HTTPURLResponse {
if httpResponse.statusCode != 200 {
let statusError = NSError(domain:"com.raywenderlich", code:httpResponse.statusCode, userInfo:[NSLocalizedDescriptionKey : "HTTP status code has unexpected value."])
completion(nil, statusError)
} else {
completion(data, nil)
//activityIndicator.stopAnimating()
}
}
} as! (Data?, URLResponse?, Error?) -> Void) //error is thrown here!!
loadDataTask.resume()
}
}
當我運行代碼,我得到的最後一行的錯誤代碼:「線程1:EXEC_BAD_INSTRUCTION(代碼= EXC_I386_INVOP,子碼=爲0x0)」
它應該是錯誤,而不是NSError –
是的,這工作。一旦我改變爲錯誤,我可以離開下來鑄造。不知道爲什麼?無論如何,它的作品。你能把這個作爲你的正式答案,以便我能打勾。謝謝 – kangarooChris