How to transfer an error from swift to objective-c with description ?

Hi everyone,


I am building a swift-based library, that can load configuration files.

Of course, configuration files can contain errors, and I need to provide helpful feedback in such cases.

I have declared error types, that inherits NSError, and I throw them with the required description when an error occures. It works great in swift.


But when I use my library in Objective-C, the errors I receive does not contain any helpful information : userInfo is nil, the only thing I have is the domain and the code.


What have I missed ? What should I do to have the userInfo transfered from the swift world to the objective-C ?


Thanks for your help !


Jerome

Accepted Answer

It seems the current Swift runtime converts ErrorType object to NSError object by retrieving only domain and code, unless the ErrorType object is an instance of NSError. Even if the ErrorType class inherits NSError, this happens.


So, if you want to pass userInfo to Objective-C part, you need to use NSError directly. Extensions would work.

Also you better send a Bug Report.

Thanks millions OOPer, you made my day :-).


I reported that problem with details and a demo project as rdar://23287003

One other option is to use NSError’s fancy new user info value provider support. You can then throw your Swift error and receive a callback when folks try to fetch the localised description and so on.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Yep, I saw those new APIs (setUserInfoValueProviderForDomain and userInfoValueProviderForDomain).

I cannot use them as they are only available in 10.11, and I also want to target earlier versions, but this new possibility is worth a mention, indeed.


Jerome

How to transfer an error from swift to objective-c with description ?
 
 
Q