Cannot find protocol declaration

Objective CCocoa TouchProtocols

Objective C Problem Overview


I have two view controllers A and B, and they both have each other as their delegates.

When I did nothing except define the protocols at the beginning of the header files and #import the other's header file, I got two errors along the lines of -

> cannot find protocol declaration for "BDelegate", which was showing in > A.h (where I wrote ) cannot find protocol declaration for > "ADelegate", which was showing in B.h (where I wrote )

Looking online, people had written earlier that the circular inclusion of header files could be leading to the problems. They recommended either using #include instead, or @class declaration like -

@class A

instead of

#import A.h

inside #import B.h

I have tried almost every combination of these imports, and @classes, and #include but still can't get rid of the warnings. Also, solutions online recommended moving the #import to the .m files but that didn't help either. Part of the reason is that the solutions online are kinda fuzzy - if you could break it down that would be great.

Any suggestions about what can be done to fix this?


-- BigViewController.h --

#import "BaseViewController.h"
#include "BaseViewController.h"

@class BigViewController;

@protocol BigViewControllerDelegate
-(void) BigViewController:(BigViewController *) bigView;
@end

@interface BigViewController : UIViewController <BaseViewControllerDelegate>
{    
     //delegate
     id <BigViewControllerDelegate> delegate;

ivars...	
}

@properties...
@end

--------------------------------------------------

-- BaseViewController.h --

#<UIKit/UIKit.h>

#import "BigViewController.h"
#include "BigViewController.h"

@class BigViewController;

@protocol BaseViewControllerDelegate
- (void) setParametersWithItemChosen:(Item *) item;
@end

@interface BaseViewController : UIViewController <...BigViewControllerDelegate...>
{

   ivars...

   //delegate
    id <BaseViewControllerDelegate> delegate;
}

@properties...
@end

Objective C Solutions


Solution 1 - Objective C

Let me reduce the sample even further, and label the lines:

VC1.h

#import "VC2.h"  // A

@class VC1;

@protocol VC1Delegate // B
@end

@interface VC1 : UIViewController <VC2Delegate> // C
@end

VC2.h

#import "VC1.h"  // D

@class VC2;

@protocol VC2Delegate // E
@end

@interface VC2 : UIViewController <VC1Delegate> // F
@end

Consider what happens when something #imports VC1.h: It reaches line A, then the import is processed. Line D does nothing because VC1.h was already imported. Then line E is processed. Then line F, and we get an error because we haven't gotten to line B yet so the protocol is not declared!

Consider then what happens when something #imports VC2.h: It reaches line D, then the import is processed. Line A does nothing because VC2.h was already imported. Then line B is processed. Then line C, and we get an error because we haven't gotten to line E yet so the protocol is not declared!

The first step is to reconsider whether both of these classes really need to be each other's delegates. If you can break the cycle, that would probably be the way to go. If not, you'll need to restructure your headers. The most straightforward way is probably to put the delegates into their own headers:

VC1Delegate.h

@class VC1;

@protocol VC1Delegate // B
@end

VC1.h

#import "VC1Delegate.h"
#import "VC2Delegate.h"

@interface VC1 : UIViewController <VC2Delegate> // C
@end

VC2Delegate.h

@class VC2;

@protocol VC2Delegate // E
@end

VC2.h

#import "VC1Delegate.h"
#import "VC2Delegate.h"

@interface VC2 : UIViewController <VC1Delegate> // F
@end

If you trace through the imports now, you'll see that the appropriate protocols will now always be declared before the @interface lines try to use them.

Solution 2 - Objective C

Write protocol declaration code above the #import lines e.g.

@protocol -----

@end

import ----

@interface classname ---

Solution 3 - Objective C

I had almost the same problem, and I fixed it thanks to the answer above, but in a slightly different way.

all I did was to put the #import line after the protocol declaration in the header file. hope I can help. and if anyone know that this is bad programing for some reason, pls let me know

Solution 4 - Objective C

I found another solution to this issue because I didn't really like the idea of just having some #imports in between the class and the protocol declaration.

Basically you just move <YourProtocolName> from the .h class file to the class extension in the .m file

So in your .m file you add

@interface YourClassName () <YourProtocolName>
@end

I don't know if this is really a good practise but it looks like a cleaner solution to avoid the import cycles.

Solution 5 - Objective C

Try putting the < BaseViewControllerDelegate > or < BigViewControllerDelegate > in implementation file rather then header file. It will work.

Solution 6 - Objective C

I followed the fix of moving the protocol before the import and it fixed the problem... the import included the delegate, so that was causing the problem.

But then I thought, why was I importing the delegate anyway? I wasn't referencing its properties and I wasn't calling any of its methods directly (that's what the protocol declare was for).

I tried commenting out the import of the delegate and saw where the error came up and found that what I was importing when I was importing the delegate was actually a declaration that the delegate was importing i.e. I was importing A (also my delegate), A was importing B, what I was actually using was B. So I left the import of A commented out and added an import for B. Then I could put the import-protocol order back the way it was.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAk1View Question on Stackoverflow
Solution 1 - Objective CAnomieView Answer on Stackoverflow
Solution 2 - Objective CVinnerView Answer on Stackoverflow
Solution 3 - Objective ClitovView Answer on Stackoverflow
Solution 4 - Objective CPotajedehabichuelasView Answer on Stackoverflow
Solution 5 - Objective CcoreDeviOSView Answer on Stackoverflow
Solution 6 - Objective CdawidView Answer on Stackoverflow