Cannot find protocol declaration for
Asked Answered
P

3

27

I have two objects, both of which are view controllers. The first (Ill call it viewController1) declares a protocol. The second (which unsurprisingly I will name viewController2) conforms to this protocol.

XCode is giving me a build error of: 'Cannot find protocol declaration for viewController1'

I have seen various questions on this subject and I am certain it is to do with a loop error, but I just can't see it in my case...

Code below..

viewController1.h

@protocol viewController1Delegate;

#import "viewController2.h"

@interface viewController1 {

}

@end

@protocol viewController1Delegate <NSObject>

// Some methods

@end

viewController2.h

#import "viewController1.h"

@interface viewController2 <viewController1Delegate> {

}

@end

Initially, I had the import line in viewController1 above that of the protocol declaration. This was preventing the project from building at all. After searching on SO, I realised the problem and switched the two lines around. I am now getting a warning (as opposed to an error). The project builds fine and actually runs perfectly. But I still feel there must be something wrong to be given a warning.

Now, as far as I can see, when the compiler gets to viewController1.h, the first thing it sees is the declaration of the protocol. It then imports the viewController.h file and sees this implements this protocol.

If it were compiling them the other way around, it would look at viewController2.h first, and the first thing it would do is import viewController1.h the first line of which is the protocol declaration.

Am I missing something?

Penumbra answered 15/4, 2012 at 9:25 Comment(0)
A
68

Remove this line from viewController1.h:

#import "viewController2.h"

The problem is that viewController2's interface is preprocessed before the protocol declaration.

The general structure of the file should be like this:

@protocol viewController1Delegate;
@class viewController2;

@interface viewController1
@end

@protocol viewController1Delegate <NSObject>
@end
Aboutface answered 15/4, 2012 at 9:36 Comment(2)
I can't...(I should have said)...viewController1 does need to be able to present a viewController2.Penumbra
There's @class viewController2; directive for that. Import the header file in the viewController1.m.Aboutface
F
1
    A.h:
    #import "B.h"  // A

    @class A;

    @protocol Delegate_A 
       (method....)
    @end

    @interface ViewController : A
    @property(nonatomic,strong)id<ViewControllerDelegate> preViewController_B;(protocol A)
    @end


    B.h:
    #import "A.h"  // A

    @class B;

    @protocol Delegate_B 
       (method....)
    @end

    @interface ViewController : B
    @property(nonatomic,strong)id<ViewControllerDelegate> preViewController_A;(protocol B)
    @end

    A.m:
    @interface A ()<preViewController_B>
    @end

    @implementation A
    (implement protocol....)
    end


    B.m:
    @interface B ()<preViewController_A>
    @end

    @implementation B
    (implement protocol....)
   @end
Furness answered 15/7, 2016 at 3:5 Comment(1)
Can you add some comments or detail? It would improve the quality of your answer and better educate everyone.Myopic
F
1

For those who might need it:

It's also possible to fix this by moving the importation of ViewController1.h in ViewController2's implementation file (.m) instead of the header file (.h).

Like so:

ViewController1.h

#import ViewController2.h

@interface ViewController1 : UIViewController <ViewController2Delegate>
@end

ViewController2.h

@protocol ViewController2Delegate;

@interface ViewController2
@end

ViewController2.m

#import ViewController2.h
#import ViewController1.h

@implementation ViewController2
@end

This will fix the case where the error happens because ViewController1.h is imported in ViewController2.h before the protocol declaration.

Flexible answered 27/6, 2017 at 16:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.