我有这段代码:
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(
data, response, error) in
guard let _:Data = data, let _:URLResponse = response , error == nil else {
print("error")
return
}
let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
result = dataString as? String ?? ""
print("Data received is: " + result)
if (result == "failed") {
displayAlertMessage(userMessage: "Your credentials are incorrect. Please check your email and password.") //ERROR
//ERROR ^
print("Cannot login.")
} else {
// Store the userID for local use
let userID = result
UserDefaults.standard.set(userID, forKey:"userID")
UserDefaults.standard.synchronize()
// Since login is successful, proceed to Prime page
DispatchQueue.main.async() {
self.performSegue(withIdentifier: "goToPrimePage", sender: self)
}
print("Can login.")
}
})
错误一直告诉我要添加自己。到那个错误行。但在添加self后,程序崩溃并显示消息
由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:' - [UIKeyboardTaskQueue waitUntilAllTasksAreFinished]只能从主线程调用。'
我对swift很新,所以我可能不理解很多使用的术语。
答案 0 :(得分:1)
添加self并确保在向用户显示任何警告消息之前跳回主线程。
DispatchQueue.main.async() {
self.displayAlertMessage(userMessage: "Your credentials are incorrect. Please check your email and password.")
}
答案 1 :(得分:0)
参考Rob对已删除解决方案的评论,但这有效:
DispatchQueue.main.async() {
self.displayAlertMessage(userMessage: "Your credentials are incorrect. Please check your email and password.")
}
不知道如何或为什么,谢谢“弥迦”。