Anand Anand - 10 days ago 6
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;

@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?

JAL JAL
Answer

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