Ios 如何从NSError.description获取[Error]?
我正在尝试创建一个Ios 如何从NSError.description获取[Error]?,ios,xcode,swift,nserror,Ios,Xcode,Swift,Nserror,我正在尝试创建一个UIAlertView,它只显示NSError.description的错误 例如: var alert = UIAlertView(title: "Oops!", message: error.description, delegate: self, cancelButtonTitle: "OK") alert.show() 说出错误。说明输出: 2015-02-01 02:26:43.227 UserLogin[249:13655] [Error]: missing us
UIAlertView
,它只显示NSError.description
的错误
例如:
var alert = UIAlertView(title: "Oops!", message: error.description, delegate: self, cancelButtonTitle: "OK")
alert.show()
说出错误。说明
输出:
2015-02-01 02:26:43.227 UserLogin[249:13655] [Error]: missing username (Code: 200, Version: 1.6.2)
我如何才能在字符串中获取[错误]:缺少用户名?我是应用程序开发新手,我只是希望我的错误消息对用户有意义并且简单
如果这没有意义,我正在使用Parse,并且正在处理注册表。如果出现诸如“缺少用户名”、“使用用户名”等错误,我只是想找到一种方法来获得一个简单的错误弹出窗口,显示“使用用户名”。您可以使用NSError
的属性
使用error.localizedDescription
var alert = UIAlertView(title: "Oops!", message: error.localizedDescription, delegate: self, cancelButtonTitle: "OK")
alert.show()
注意:
在iOS 8中已弃用,请改用。您可以使用NSError的属性
使用error.localizedDescription
var alert = UIAlertView(title: "Oops!", message: error.localizedDescription, delegate: self, cancelButtonTitle: "OK")
alert.show()
注意:
在iOS 8中已弃用,请改用。尝试使用以下方法:
if let dic = error?.userInfo {
let errorString = dic[NSUnderlyingErrorKey]?.localizedDescription
var alert = UIAlertView(title: "Oops!", message:errorString, delegate: self, cancelButtonTitle: "OK")
alert.show()
}
尝试使用以下方法:
if let dic = error?.userInfo {
let errorString = dic[NSUnderlyingErrorKey]?.localizedDescription
var alert = UIAlertView(title: "Oops!", message:errorString, delegate: self, cancelButtonTitle: "OK")
alert.show()
}
在您向这个方向前进之前,您应该非常非常仔细地思考您收到的N错误是否意味着用户可以看到。许多N错误只能由程序员检查。在这种情况下,“缺少用户名”似乎是一个强烈的迹象,表明您,程序员,发送了一个没有用户名的请求,犯了一个错误,并且一开始就不应该这样做。因此,显示错误警报可能是完全错误的解决方案 在您向这个方向前进之前,您应该非常非常仔细地思考您收到的N错误是否是为了让用户看到。许多N错误只能由程序员检查。在这种情况下,“缺少用户名”似乎是一个强烈的迹象,表明您,程序员,发送了一个没有用户名的请求,犯了一个错误,并且一开始就不应该这样做。因此,显示错误警报可能是完全错误的解决方案