2017-09-03 70 views
0

我試圖在密碼匹配時顯示警報消息。一切都運行得非常好:警報 - libC++ abi.dylib:以NSException類型的未捕獲異常終止

1)Web服務調用,並返回密碼

2)流量進入,如果條件涉及的比賽中密碼

if(userPasswd == sysPasswd) 
    { 
     displayAlert(userMessage: "Welcome! You have been authenticated") 
     return 
    } 

的displayAlert函數被調用成功:

func displayAlert(userMessage: String) 
    { 
    // create the alert 
    let myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.alert) 

    // add an action (button) 
    myAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) 

    // show the alert 
    self.present(myAlert, animated: true, completion: nil) 
    } 

當執行的最後一條語句它給這個錯誤

libc++abi.dylib: terminating with uncaught exception of type NSException 

這是怎麼發生的?我是斯威夫特的初學者,仍然在學習繩索。沒有黃色標誌等。

+0

當網絡方法返回時,你確定'self'存在嗎? – the4kman

+0

在調試器中設置異常斷點以查看確切的錯誤消息。 –

+0

你是否在顯示警報時在主線程中?我懷疑你不是.. –

回答

0

用戶界面元素只能在主線程上使用。如果您嘗試從後臺線程更新用戶界面,則需要在調用DispatchQueue.main.async時將用戶界面調用包裝起來。

func displayAlert() { 
    DispatchQueue.main.async { 
     let myAlert = UIAlertController(etc...) 
     ... 
    } 
} 
+0

謝謝。得到它了。 – coderatlarge

相關問題