Swift class does not conform to Objective-C error handling protocol

I have an Objective-C Protocol

 @protocol SomeObjCProtocol - (BOOL) doSomethingWithError: (NSError **)error; @end 

And Swift class

 class SwiftClass : SomeObjCProtocol { func doSomething() throws { } } 

Compilers give me error

Type "SwiftClass" does not conform to the protocol "SomeObjCProtocol" "

Is there any solution how to get rid of this error? I am using XCode 7 Beta 4

+4
source share
2 answers

There are two problems:

  • Swift 2 maps func doSomething() throws to the Objective-C method - (BOOL) doSomethingAndReturnError: (NSError **)error; that is different from your protocol.
  • The protocol method should be marked as "Objective-C compatible" with the @objc attribute.

There are two possible solutions:

Solution 1: Rename the Objective-C protocol method to

 @protocol SomeObjCProtocol - (BOOL) doSomethingAndReturnError: (NSError **)error; @end 

Solution 2: Leave the Objective-C protocol method as it is, and specify the Objective-C mapping for the Swift method explicitly:

 @objc(doSomethingWithError:) func doSomething() throws { // Do stuff } 
+6
source

If you encounter this error message, one of the sources of the problem may be that the Swift class corresponding to the Objetive C protocol was not inherited from NSObject.

0
source

All Articles