簡體   English   中英

如何從NSError.description中獲取[錯誤] :?

[英]How can I get the [Error]: from NSError.description?

我試圖創建僅顯示NSError.description錯誤的UIAlertView

例如:

var alert = UIAlertView(title: "Oops!", message: error.description, delegate: self, cancelButtonTitle: "OK")
alert.show()

說出error.description輸出:

2015-02-01 02:26:43.227 UserLogin[249:13655] [Error]: missing username (Code: 200, Version: 1.6.2)

我怎樣才能得到[Error]: missing username String [Error]: missing username 我是應用程序開發的新手,我只想讓我的錯誤消息對用戶有意義且簡單。

如果這沒有意義,則說明我正在使用Parse,並且正在處理注冊表。 如果發生錯誤,例如“缺少用戶名”“采用用戶名”等,我只是想找到一種方法來獲取簡單的錯誤彈出窗口,說“采用用戶名”

您可以使用NSErrorlocalizedDescription屬性。

代替error.description使用error.localizedDescription

var alert = UIAlertView(title: "Oops!", message: error.localizedDescription, delegate: self, cancelButtonTitle: "OK")
alert.show()

注意:

UIAlertView在iOS 8中已棄用,請改用UIAlertController

嘗試使用此:

if let dic  = error?.userInfo {
   let errorString = dic[NSUnderlyingErrorKey]?.localizedDescription
   var alert = UIAlertView(title: "Oops!", message:errorString, delegate: self, cancelButtonTitle: "OK")
   alert.show()
}

在朝這個方向發展之前,您應該非常非常認真地思考是否收到的NSError是否應該被用戶看到。 許多NSError僅應由程序員檢查。 在這種情況下,“缺少用戶名”似乎有力地表明,程序員您通過發送不帶用戶名的請求而犯了一個錯誤,因此一開始就不應該這樣做。 因此,顯示錯誤警報可能是完全錯誤的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM