CKError
encountered to the user in the app as an alert. Note: This question is not about UI code to display. Just want to extract a meaningful string from the error.
I tried to use localizedDescription but it doesn't seem to contain an appropriate string
Given below are the attempts I made:
po error
<CKError 0x1c464cea0: "Network Unavailable" (3/NSURLErrorDomain:-1009); "The Internet connection appears to be offline.">
po error.localizedDescription
"The operation couldn’t be completed. (CKErrorDomain error 3.)"
po (error as! CKError).errorUserInfo
▿ 2 elements
▿ 0 : 2 elements
- key : "NSUnderlyingError"
- value : Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={NSErrorFailingURLStringKey=https:/
▿ 1 : 2 elements
- key : "NSDebugDescription"
- value : NSURLErrorDomain: -1009
po (error as? NSError)?.localizedFailureReason
nil
po (error as? NSError)?.localizedRecoverySuggestion
nil
po (error as? NSError)?.localizedRecoveryOptions
nil
po (error as? NSError)?.debugDescription
▿ Optional<String>
- some : "<CKError 0x1c064eaf0: \"Network Unavailable\" (3/NSURLErrorDomain:-1009); \"The Internet connection appears to be offline.\">"
The debug description seems to be the closest to what I want.
A string containing the localized description of the error.
The core attributes of an NSError object are an error domain (represented by a string), a domain-specific error code and a user info dictionary containing application specific information.
Looks like there is another error in the errorUserInfo[NSUnderlyingError]. Try getting the localizedDescription from that error.
So, that would be:
((error as? CKError)?.errorUserInfo[NSUnderlyingErrorKey] as? NSError)?.localizedDescription
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