"Execution was interrupted, reasno: EXC_BAD_INSTRUCTION"错误,因为我尝试发出 GET 请求



我正在尝试在我的操场上运行这个:

func getWeatherForecast(){
let Endpoint : String =  "http://dataservice.accuweather.com/currentconditions/v1/{}"
let url = URL(string:Endpoint)
let parameters = ["apikey":"API_KEY"] as [String:Any?]
var urlRequest = URLRequest(url:url!)
do
{urlRequest.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
} catch {
print("error")
}
let task = URLSession.shared.dataTask(with: urlRequest) {(data, response, error) in
do {
let json = try JSONSerialization.jsonObject(with: data!, options: [])
print("The Response is : ",json)
} catch {
print("JSON error: (error.localizedDescription)")
}
}
task.resume()
}
getWeatherForecast()

然后我不断收到以下两个错误:

error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION 
(code=EXC_I386_INVOP, subcode=0x0).
The process has been left at the point where it was interrupted, use 
"thread return -x" to return to the state before expression 

我已经尝试了许多运行此 API 调用的变体,但我无法弄清楚我的代码出了什么问题。有人可以在这里帮忙吗?我只是想做一个简单的 GET,但它似乎一直在告诉我"致命错误:在解开可选值时意外发现 nil"我觉得我已经正确解开了包装。

任何帮助将不胜感激。谢谢!

您正在调用被Apple阻止的"http"网址,因为它不安全。您必须在 plist 文件中使用"https"或临时允许它。

最新更新