Swift 3 conversion Error/NSError

Anand picture Anand · Sep 11, 2016 · Viewed 8.3k times · Source

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;

@end

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

JAL picture JAL · Nov 29, 2016

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")
    }
}