Skip to content

Instantly share code, notes, and snippets.

@rtking1993
Last active January 22, 2018 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtking1993/26a4d36a69563f38f5e726949b6edd67 to your computer and use it in GitHub Desktop.
Save rtking1993/26a4d36a69563f38f5e726949b6edd67 to your computer and use it in GitHub Desktop.
UIAlertController+Extension
// Extension
extension UIAlertController {
convenience init(with error: Error) {
var title: String!
var message: String!
if let error = error as? LocalizedError, let errorDescription = error.errorDescription {
title = errorDescription
if let recoverySuggestion = error.recoverySuggestion {
message = recoverySuggestion
}
} else {
let nativeError = error as NSError
title = nativeError.localizedDescription
message = nativeError.localizedRecoverySuggestion ?? ""
}
self.init(title: title, message: message, preferredStyle: .alert)
addOKAction()
}
func addOKAction() {
addAction(UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .cancel, handler: nil))
}
}
// Implementation
let alert = UIAlertController(with: error)
self.present(alert, animated: true, completion: nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment