JAH-FH
JAH-FH

Reputation: 119

Objective C class cannot find definition for Swift protocol

I have a Swift protocol, like so:

@objc protocol Linkable {
    func presentLink(link: MyLink)
}

I also have an Objective-C class that isn't playing nice with this protocol:

@protocol Linkable;

@interface MyDetailViewController : MyTableViewController <Linkable>

etc...

I have declared the protocol correctly as far as I can tell, the protocol has the @objc notation, and I'm putting it into the interface declaration of the Objective C class, but I'm still getting a warning at the line that starts @interface.

The warning says: "Cannot find protocol definition for 'Linkable'."

Oddly, it builds and runs, and it works as expected, but why the warning if there's actually no problem with Linkable? Is there a different way to declare the protocol, or to conform to it that would clear the warning?

Is this just one of Xcode's warnings that is poorly phrased, and if so what's actually going on?

EDIT

Here is a self-contained sample project with the same error: https://github.com/thinkfishhook/Swift-ObjC_Protocol

Upvotes: 9

Views: 2596

Answers (2)

This is an unfortunate side effect of not being able to import the Swift bridging header file in an Objective-C header.

The code will work just fine as you have found out, so it is safe to suppress the warning with

@protocol Linkable;

// To get rid of 'cannot find protocol definition for' warnings
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wprotocol" 
@interface MyDetailViewController : MyTableViewController <Linkable>
#pragma clang diagnostic pop

Upvotes: 0

Christopher Moore
Christopher Moore

Reputation: 149

#import "ViewController.h"
#import "Protocol_WTF-Swift.h"

@interface ViewController () <MyProtocol>

@end

@implementation ViewController

- (void)doTheThings {
  // doing the things
}

@end

Upvotes: 1

Related Questions