objective-cswiftobjective-c-protocolobjective-c-swift-bridge

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


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?


Solution

  • 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.