当发生错误时,向用户显示有意义消息的最常见格式是什么?Swift



发生错误时,向用户显示有意义消息的最常见方式是什么?

我使用CoreLocation来确定用户的位置,并尽可能地处理错误。

我有下面的代码,如果发生错误,它会显示一条Alert消息。

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error)-> Void in
/// Show ERROR
if error != nil {               
let alert = UIAlertController(title: "Error", message: error!.localizedDescription.description, preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: { action in
/// stop updating
self.locationManager.stopUpdatingLocation()
return
}))
self.present(alert, animated: true, completion: nil)
return
}
if placemarks!.count > 0 {
// do stuff here with the found placemark
}else{
print("No placemarks found.")
}
})
}

例如,如果没有互联网连接,用户将看到以下错误。。。

错误:操作无法完成。(kCLError域错误2。(

这是你通常向用户显示错误的方式,还是你会用一些通用消息代替错误消息,比如。。。"很抱歉,我们无法连接到服务器。请确保您已连接到internet"?

谢谢。

通常错误消息应该是用户能够容易理解的。

类似的错误

Error: The operation could't be completed. (kCLErrorDomain error 2.)

通常是为了让开发人员了解为什么会发生这种错误。它是由编译器提供的,不适合按原样呈现给用户。因此,对于实时应用程序,最好对所有错误消息使用通用消息&还有成功的信息。

是的,UIAlertController可能是向用户显示错误的最标准方式,除非你想花点心思制作自己的自定义错误子视图。

UI设计的10种启发式方法之一指出:

错误消息应该用简单的语言(没有代码(表示,准确地指出问题,并建设性地提出解决方案。

因此,我建议您为这个实例编写自己的错误代码,例如"There was an error updating your location",而不是使用error.localizedDescription.description。这将提高应用程序对用户的可用性。

最新更新