为什么要使用结构的对话框拨打对话框丢弃此例外?
我试图重定向在结构中解码的 API 调用的输出,但是当我尝试使用数据创建对话框时,它给了我这个奇怪的异常。如您所见,API 返回数据,但仅当我创建对话框时我才会看到此异常。你能帮助我吗?
代码:
struct rspServerInfo: Codable{
let ok: Bool
let info: String
}
@IBAction func backendDetails(_ sender: Any) {
guard let url = URL(string: "http://\(hostname):\(port)/STOInfo/ServerInfo")else{
return
}
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let body: [String: AnyHashable] = [
"username": username,
"password": password,
]
request.httpBody = try? JSONSerialization.data(withJSONObject: body, options: .fragmentsAllowed)
let task = URLSession.shared.dataTask(with: request) {data, _, error in
guard let data=data, error == nil else{
return
}
do{
let response = try JSONDecoder().decode(rspServerInfo.self, from: data)
print("SUCCESS: \(response)")
let dialogMessage = UIAlertController(title: "Backend details", message: response.info, preferredStyle: .alert)
let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
print("Ok button tapped")
})
dialogMessage.addAction(ok)
self.present(dialogMessage, animated: true, completion: nil)
}
catch{
print(error)
let dialogMessage = UIAlertController(title: "Backend details", message: "Error retreiving.", preferredStyle: .alert)
let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
print("Ok button tapped")
})
dialogMessage.addAction(ok)
self.present(dialogMessage, animated: true, completion: nil)
}
}
task.resume()
}
I'm trying to redirect the output of this API Call decoded in a struct but when I try to use the data to create a dialog it gives me this weird exception. As you can see, the API returns data but only when I create the dialog I see this exception. Can you help me?
Code:
struct rspServerInfo: Codable{
let ok: Bool
let info: String
}
@IBAction func backendDetails(_ sender: Any) {
guard let url = URL(string: "http://\(hostname):\(port)/STOInfo/ServerInfo")else{
return
}
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let body: [String: AnyHashable] = [
"username": username,
"password": password,
]
request.httpBody = try? JSONSerialization.data(withJSONObject: body, options: .fragmentsAllowed)
let task = URLSession.shared.dataTask(with: request) {data, _, error in
guard let data=data, error == nil else{
return
}
do{
let response = try JSONDecoder().decode(rspServerInfo.self, from: data)
print("SUCCESS: \(response)")
let dialogMessage = UIAlertController(title: "Backend details", message: response.info, preferredStyle: .alert)
let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
print("Ok button tapped")
})
dialogMessage.addAction(ok)
self.present(dialogMessage, animated: true, completion: nil)
}
catch{
print(error)
let dialogMessage = UIAlertController(title: "Backend details", message: "Error retreiving.", preferredStyle: .alert)
let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
print("Ok button tapped")
})
dialogMessage.addAction(ok)
self.present(dialogMessage, animated: true, completion: nil)
}
}
task.resume()
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误消息给出了一个很大的线索:
urlrequest在背景线程(包括其完成处理程序)上不同步运行。您(通常)无法在主线程之外进行UI工作。要显示结果,您需要将操作重新推回主线程:
The error message give a big clue:
The URLRequest run asynchronously on a background thread, including its completion handlers. You (generally) can't do UI work outside of the main thread. To display the results you need to push the operation back onto the main thread: