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

3
  • @protocol DelegateOfObject1 <NSObject> @optional - (void)someThingHappensAt:(NSString *)onWhatStuff; @end Commented Apr 29, 2010 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. Commented May 6, 2015 at 19:17
  • Please check this solution stackoverflow.com/questions/6447573/… Commented Apr 8, 2017 at 11:54

5 Answers 5

56

The error is caused due to import loop.

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

4 Comments

Cheers, that was causing me a headache!
-1, No explanation. Explain what an import loop is, how to fix it.
Sam - import loop is when 2 modules each import each other. You don't know which one the compiler is going to hit first. One of the ways you can avoid this is to have one of them declare the other as an @class.
11

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

1 Comment

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.
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. :)

Comments

2

Are you doing an

#import "NameOfDelegate.h" 

At the top of your MyObject header?

1 Comment

I've added the neccesarry .h file
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.