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

1

26

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?

Noncompliance answered 2/10, 2015 at 20:37 Comment(0)
N
47

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.

Noncompliance answered 2/10, 2015 at 20:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.