Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type "SwiftClass" cannot conform to protocol "ObjcProtocol" because it has requirements that cannot be satisfied

Tags:

I have an Objective-C protocol which I'm trying to implement in a Swift class. For example:

@class AnObjcClass;  @protocol ObjcProtocol <NSObject>     - (void)somethingWithAnArgument:(AnObjcClass *)arg; @end 

When I try to conform to it in a Swift class like this:

@objc class SwiftClass: NSObject, ObjcProtocol {     // ... } 

I get the following scary compiler error:

Type "SwiftClass" cannot conform to protocol "ObjcProtocol" because it has requirements that cannot be satisfied.

How do I resolve this?

like image 762
Jesse Rusak Avatar asked Oct 02 '15 20:10

Jesse Rusak


1 Answers

Ensure any classes referenced by that protocol are included in your bridging header.

This error happens when one of the types used in the protocol (the protocol itself, a return type, an argument type) is not included in your Swift bridging header.

Objective-C classes can happily implement this protocol because of the @class AnObjcClass forward declaration, but it appears that Swift classes can't implement protocols which use classes that are only forward-declared.

like image 157
Jesse Rusak Avatar answered Sep 20 '22 17:09

Jesse Rusak