14

I have declared a protocol firstly, and then use it. But I get a warning "Cannot find protocol definition for LeveyPopListViewDelegate". Here is the code:

@protocol LeveyPopListViewDelegate; @interface LeveyPopListView : UIView <LeveyPopListViewDelegate,UITableViewDataSource, UITableViewDelegate,UITextFieldDelegate> //the content of LeveyPopListView @end @protocol LeveyPopListViewDelegate <NSObject> //the definition for LeveyPopListViewDelegate @end 

if I put the definition LeveyPopListViewDelegate at first, I can not use the LeveyPopListView in the protocol.

5
  • 3
    Max_: While some things we used to do with delegates are now better done with blocks, there are LOTS of reasons to use protocols. Commented Feb 21, 2013 at 2:04
  • which version of XCode are you on? Commented Feb 21, 2013 at 2:10
  • Xcode Version 4.6 (4H127) Commented Feb 21, 2013 at 2:25
  • not that it matters... your code is running fine without errors for me (XCode4.6) - I just tried it in some older versions in case something had changed, but it's ok also in 4.2 ;-j Commented Feb 21, 2013 at 2:36
  • But the weird thing is that despite the warning it does work, right? Commented Mar 16, 2018 at 11:20

7 Answers 7

16

I ended up suppressing all warnings for that particular line, which is not ideal but works well.

// Forward-declare protocols (to avoid circular inclusion) @protocol YourProtocol; #pragma clang diagnostic push // To get rid of 'No protocol definition found' warnings which are not accurate #pragma clang diagnostic ignored "-Weverything" @interface YourClass: NSObject // <YourProtocol> #pragma clang diagnostic pop 

Make sure you do push & pop, otherwise you'll end up with all warnings being ignored for this file!

Sign up to request clarification or add additional context in comments.

1 Comment

That did the trick. As I am using a Swift protocol, that was the only way to remove this warning. I also found a good source here in this blogpost from NSHipster
2

I always do it this way:

@class LeveyPopListView; @protocol LeveyPopListViewDelegate <NSObject> //the definition for LeveyPopListViewDelegate @end @interface LeveyPopListView : UIView <LeveyPopListViewDelegate,UITableViewDataSource, UITableViewDelegate,UITextFieldDelegate> //the content of LeveyPopListView @end 

4 Comments

But I want to use LeveyPopListView in protocol LeveyPopListViewDelegate
You can with what I posted. That's the point of the forward class declaration.
Thank you very much. But why declaring a protocol first will get a warning?
I've never been able to make forward protocol definitions work so I always do as I posted.
2

If you followed the good advice in this post and still have the issue try just doing a clean. I was scratching my head over it for a while and a clean resolved the issue.

Comments

1

This happened to me when I imported Swift protocol to Objective C class. What helped me is to add *-Swift.h file to the Objective C class file:

#import "MyApp-Swift.h" 

and declare protocol as @objc:

@objc protocol VerificationGate 

1 Comment

I am curious how that worked out. I have the same warning. It is not possible to import the automatically generated Swift bridging header in Objective-C headers as it is not generated yet. It can only be imported in Objective-C implementation files. But the protocol conformance is defined in Objective-C headers. A forward declaration in the Objective-C header does not help.
1

Also, if you declared and defined protocol in different file, you have to #import that file, forward declaration won't help in this situation.

BTW. Never suppress warnings as some answers suggest.

Comments

0

It's unusual having to use a concrete class name in a protocol definition. What you should do (most of the cases, exceptions may apply… though they are a bad smell) is make your class conform to a protocol, and use that class in the delegate protocol definition.

It is also a bad smell that a class needs to be its own delegate. I'd revise the design carefully.

Comments

-1

You have to define the protocol first. Try like below

@protocol LeveyPopListViewDelegate <NSObject> //the definition for LeveyPopListViewDelegate @end @interface LeveyPopListView : UIView <LeveyPopListViewDelegate,UITableViewDataSource, UITableViewDelegate,UITextFieldDelegate> //the content of LeveyPopListView @end 

2 Comments

But I want to use LeveyPopListView in protocol LeveyPopListViewDelegate
You can forward declare LeveyPopListView above the protocol definition. i.e @class LeveyPopListView;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.