I'm trying to find an easier/more reliable way to figure out an NSError's localized description from its error code than Google searching it.
For example, I know that NSURLErrorDomain code -1003 corresponds to "A server with the specified hostname could not be found". But if I try to verify it in code it doesn't match.
let error = NSError(domain: "NSURLErrorDomain", code: -1003)
print(error.localizedDescription)
// "The operation couldn’t be completed. (NSURLErrorDomain error -1003.)"
Looking up -1003 in documentation also doesn't match: "The host name for a URL couldn’t be resolved."
So I am looking for a way to figure out descriptions from an error code with a function, or the documentation that has the description I expected. I was hoping there'd be a function similar to HTTPURLResponse.localizedString(forStatusCode:)
When you create your own
NSErrorobject like that, thatlocalizedDescriptionis not generated for you. WhenURLSessiongenerates error objects, though, the localized description is populated:So, if you have an error and want to see the localized description, just do that. It simply will not work if you create your own
NSErrorobject manually.But generally, we wouldn't worry about the localized description and, instead, test for various
codevalues ofURLError, looking for acodeof.cannotFindHost:Or, alternatively, you can search for the old
NSURLErrorcode values usingNSError, too, looking forNSURLErrorCannotFindHost:You can also “quick open” by pressing shift-command-O (the letter “Oh”), search for
NSURLError, uncheck the “Swift” button in the upper right corner of the quick open dialog:When you open up the
NSURLError.hfile, you can then see all the codes listed there.But, no, just by creating a
NSErrorwith the specified domain and code, thelocalizedDescriptionisn't magically populated for you.URLSessioncreates the proper error objects with descriptions, though.