Search code examples
objective-c

Interdependent Objects causing compiler errors


I have:

@interface A
@property (nonatomic, retain) B *toB;
@end

@interface B
@property (nonatomic, retain) A *toA;
@end

This causes the compiler to give me this:

error: expected specifier-qualifier-list before 'Property'

Now, it appears this has something to do with the order of parsing the files as independently, they work as long as the pointed to object is declared first.

How can I get round this?


Solution

  • Use forward declaration via @class to let the compiler know there is a class named A that it hasn't seen the interface for yet.

    For example:

    @class A;
    @class B;
    
    @interface A
    @property (nonatomic, retain) B *toB;
    @end
    
    @interface B
    @property (nonatomic, retain) A *toA;
    @end