23

I've experienced something today while I'm building my app. I've declared a protocol in my MyObject1 and add delegate property on it. I've assign MyObject2 as a Delegate of the MyObject1. I've added it in this way as usual

@interface MyObject2 : UIViewController <DelegateOfObject1>

But the Xcode says that my the protocol declaration cannot be found. I've check my code but I've declared this protocol. I've try to assign MyObject2 as delegate of other Object. I've edit my code like this

@interface MyObject2 : UIViewController <UITableViewDelegate,DelegateOfObject1>

but Xcode say again that it cannot found declaration of protocol of DelegateOfObject1. I've tried to delete the DelegateOfObject1 on my code and add assign MyObject as delegate of other object and it goes like this.

@interface MyObject2 : UIViewController <UITableViewDelegate,UITabBarDelegate>

No errors have been found. Then I've tried again to add again my DelegateOfObject1 in the code

@interface MyObject2 : UIViewController <UITableViewDelegate,UITabBarDelegate,DelegateOfObject1>

At that time Xcode did not find any error on my code. So I tried again to remove the UITableViewDelegate and UITabBarDelegate on my code.

@interface MyObject2 : UIViewController <DelegateOfObject1>

At that time No error had found but that was the same code I've write before. What should probably the cause of that stuff on my code?

Thanks...

edie
  • 758
  • 1
  • 11
  • 29
  • `@protocol DelegateOfObject1 @optional - (void)someThingHappensAt:(NSString *)onWhatStuff; @end` – edie Apr 29 '10 at 12:26
  • FWIW I had exactly this error, but it was caused by a duplicate copy of an old version of a framework in one of the folders specified in Framework Search Paths. The old version didn't contain the protocol in question, but the containing folder came first in the search path order. Deleting the duplicate framework resolved the error. – stephent May 06 '15 at 19:17
  • Please check this solution http://stackoverflow.com/questions/6447573/cannot-find-protocol-declaration – Chandni - Systematix Apr 08 '17 at 11:54

5 Answers5

56

The error is caused due to import loop.

Valla
  • 611
  • 1
  • 5
  • 3
11

I've put my protocol declaration on separate file and import it on MyObject2

edie
  • 758
  • 1
  • 11
  • 29
  • Cut your @protocol definition out of the class. Create a new header file (`File > New > File... > Header File`, paste the @protocol definition and then import the header file. – Jason Moore Jul 06 '16 at 15:46
4

The error must be in import loop.

I had import "AppDelegate.h" in both the classes.I removed it from the class which declared protocol and the error was gone. :)

2

Are you doing an

#import "NameOfDelegate.h" 

At the top of your MyObject header?

djhworld
  • 6,726
  • 4
  • 30
  • 44
0

Use '@class MyObject;' in order to avoid import loop.

Maksim Usenko
  • 311
  • 3
  • 5