-5
我得到这个错误传递可选:斯威夫特3:致命错误:意外发现零而展开的可选值
fatal error: unexpectedly found nil while unwrapping an Optional value
这里是我的代码:
func makeRequestcompletion(completion:@escaping (_ response:Data, _ error:NSError)->Void) {
let urlString = URL(string: "https://myUrl.com")
if let url = urlString {
let task = URLSession.shared.dataTask(with: url, completionHandler: { (data, urlRequestResponse, error) in
completion((data)!, error as! NSError) // <-- here is where I'm getting the error
})
task.resume()
}
}
任何你知道这是为什么我收到这个错误?
我真的很感谢你的帮助。
的可能的复制[什么是“致命的错误:零而展开的可选值意外地发现”呢?(http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly -found-nil-while-unwrapping-an-optional-valu) – Hamish
@Hamish,不,因为我不问什么是可选的。我在问为什么数据出现错误,即使当我解开变量时 – user2924482
无关你为什么将'error'转换为'NSError'?只需使用'Error'。 – rmaddy