Reputation: 17043
Is it possible to get localized descriptions from error codes?
Exmaple: When app tries to connect in offline NSURLSession returns error:
Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline."
Is it possible to get "The Internet connection appears to be offline." description using Domain and code? Something like following (it does not really work):
let error = NSError(domain: "NSURLErrorDomain", code: -1009)
print(error.localizedDescription)
Upvotes: 3
Views: 1700
Reputation: 516
This only works when the NSLocalizedDescriptionKey is set in the user info dictionary. Apparently it is not for the NSError object that you from NSURLSession and thus a default string is constructed from the domain and code. See the NSError documentation for more details.
If you want to localize the error messages you show your users you will have to check the errorcode for the type of error and show an appropriately localized string. See Localizing Your App for more details on using localization.
Edit:
Reading your question again I think I misunderstood your question.
If you want to initialize an NSError object with domain and errorcode, and have it contain a certain error message that corresponds to the error code used by NSURLSession the answer is NO.
The specific error codes are not a globally recognized standard and nothing prevents other applications from using the same codes.
That being said NSError does not automatically populate the message fields according to specific error codes. Clients that use the NSError object have to do so manually.
Upvotes: 2