Anand Anand - 1 year ago 253
Objective-C Question

Swift 3 conversion Error/NSError

While trying to migrate to Swift 3 (in a project which contains about half/half swift/objective-c code), I am facing an issue. We declare this specific protocol in objective-c like this:

@protocol AProtocolDeclaration <NSObject>

- (void)someEventHappened:(nullable NSError *)error;


Swift compiler generates the following for the protocol declaration above:

public protocol AProtocolDeclaration : NSObjectProtocol {

public func someEventHappened(_ error: Error?)

And when implementing the protocol in a
concrete class (in swift)
and trying to define the generated method, I am keep on getting error something like : cannot convert
Error to NSError
. I'm not sure how to fix this error. Could any one please suggest?

Answer Source

I think this was a bug with SourceKit in Xcode 8. As of Xcode 8.1 and 8.2 beta 2, protocols functions contain NSError parameters are bridged correctly to Swift 3 as Error:

extension MyClass: AProtocolDeclaration {
    func someEventHappened(_ error: Error?) {
        print(error ?? "none")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download