I have a variable that is defined like as an Error and this is what it looks like when I print it:
Optional(Error Domain=com.apple.LocalAuthentication Code=-2 "Canceled by user." UserInfo={NSLocalizedDescription=Canceled by user.})
What I am trying to do is get that Code of -2...how would I do that?
You can unwrap the optional error
first and compare the -2
case.
if let error = error {
switch error._code {
case LAError.userCancel.rawValue: // or -2 if you want
// do something
default:
break
}
}
I wrote this small extension:
extension Error {
var errorCode:Int? {
return (self as NSError).code
}
}
Using:
if error.errorCode == -2 {
//some code
}
You just need to cast your Error to LAError (Local Authentication error) and switch its code property:
if let error = error as? LAError {
switch error.code {
case .userCancel:
print("userCancel")
default:
print("unknown error")
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With