Swift: Should NSError be considered a legacy now?

Docs and popular blogs suggest that Swift error handling is done using do-catch and to handle an ErrorType enumeration or an NSError instance.

Are ErrorType and NSError overflow instances mutually exclusive in a try catch block? If not, how do you implement a function that throws both?

I have associated an NSError instance with an enumeration, sort of like this, which seems to work, but is this a de facto way to return detailed error information?

enum Length : ErrorType { case NotLongEnough(NSError) case TooLong(NSError) } func myFunction() throws { throw Length.NotLongEnough(NSError(domain: "domain", code: 0, userInfo: [NSLocalizedFailureReasonErrorKey: "Not long enough mate"])) } do { try myFunction() } catch Length.NotLongEnough(let error) { print("\(error)") } 

This example shows how ErrorType can be passed to NSError.

 do { let str = try NSString(contentsOfFile: "Foo.bar", encoding: NSUTF8StringEncoding) } catch let error as NSError { print(error.localizedDescription) } 

I can not find the error forwarding that matches the ErrorType for NSString, so should we assume that this will be an instance of NSError? Of course, we could run the code to make sure, but, of course, the documents should tell us. (I appreciate that I may have read the documents incorrectly)

+5
source share
2 answers

NSError class uses the ErrorType interface, and any ErrorType -conformant class can be added to NSError . These functions are described here in the docs .

You can safely stick to ErrorType , especially if you plan to interact only with Swift.

 enum CommonError: ErrorType { case InternalInconsistency(String) } func boom() throws { throw CommonError.InternalInconsistency("Boom!") } do { try boom() } catch { print(error) // InternalInconsistency("Boom!") print(error as NSError) // Error Domain=CommonError Code=0 "(null)" } do { try boom() } catch let CommonError.InternalInconsistency(msg) { print("Error: \(msg)") // Error: Boom! } 
0
source

Any type of ErrorType can be successfully applied to NSError , which means that if you want to handle ErrorType as a priority or specific types of Swift type corresponding to ErrorType types, you should have these cases before the case when you added to NSError (which, again will succeed for all ErrorType corresponding ant types).

I'm not sure if there are any canonical opinions expressed by Apple at this stage about how to deal with this duality right now, but personally I try to ErrorType to Swift's ErrorType type matching for my own mistakes and only include casting on NSError when I want to make conditional behavior based on some Cocoa or third-party NSError code, where a domain and code combo somehow makes sense for my logic (for example, if specific errors can be filtered out of registration or responses).

Problems with existing Swift error handling tools arise mainly because they cannot determine which errors are generated by the method, in combination with ErrorType , devoid of any key contextual information included in NSError , which gives additional custom work every time you want to present ErrorType in the user interface to the user somehow. Unlike NSError, ErrorType does not have the ability to transmit information intended for the user interface, such as a “reason” or “recovery suggestion,” or even a “description”. It looks like it will not be addressed yet for Swift 3 (in a recent discussion of this issue on the Swift mailing list).

Regarding "I cannot find an error enumeration that matches ErrorType for NSString." I'm really not sure that I correctly understood this phrase or correctly formulated it as a whole, but perhaps the following is relevant:

I personally follow the agreement to create my ErrorType implementations and use the description property to describe the error message that the user read. This is by no means ideal, and especially on OSX, where NSResponder gives you the presentError method quite well, which makes a very nice and clear error dialog box if you fill out the description and recovery recommendations.

+1
source

All Articles