gpt4 book ai didi

ios - Swift 错误 - Thread1 : EXC_BAD_INSTRUCTION

转载 作者:行者123 更新时间:2023-11-28 15:13:45 24 4
gpt4 key购买 nike

在 Xcode 9 中运行我的代码后,我得到一个 Thread1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP,subcode=0x0)。该代码在 Xcode 8 中运行良好。

 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 = session.dataTask(with: url, completionHandler: { (data: Data?, response: URLResponse?, error: NSError?) -> Void in
if let responseError = error {
completion(nil, responseError)
} else if let httpResponse = response as? HTTPURLResponse {
if httpResponse.statusCode != 200 {
let statusError = NSError(domain:"Domain", code:httpResponse.statusCode, userInfo:[NSLocalizedDescriptionKey : "HTTP status code has unexpected value."])
completion(nil, statusError)
} else {
completion(data, nil)
}
}
} as! (Data?, URLResponse?, Error?) -> Void) //Thread1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP,subcode=0x0)

loadDataTask.resume()
}

我正在尝试从 url 加载数据,我不知道为什么会收到此错误。谢谢。

最佳答案

不要强制转换,只要使用正确的语法,闭包中没有类型注释,最后没有类型转换:

let loadDataTask = session.dataTask(with: url, completionHandler: { (data, response, error) in
// code
})

关于ios - Swift 错误 - Thread1 : EXC_BAD_INSTRUCTION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47212813/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com