Declaring two classes that contain instance variables of each others' types.

Posted by Benji XVI on Stack Overflow See other posts from Stack Overflow or by Benji XVI
Published on 2010-06-16T19:39:22Z Indexed on 2010/06/16 19:42 UTC
Read the original article Hit count: 134

Filed under:
|

Let’s say you wish to have two classes like so, each declaring an instance variable that is of the other’s class.

@interface A : NSObject {
    B   *something;
}
@end

@interface B : NSObject {
    A   *something;
}
@end

It seems to be impossible to declare these classes with these instance variables. In order for A to include an IV of class B, B must already be compiled, and so its @interface must come before A’s. But A’s must be put before B’s for the same reason.

Putting the two class declarations in separate files, and #import-ing each other’s .h doesn’t work either, for obvious reasons.

So, what is the solution? Or is this either (1) impossible or (2) indicative of a bad design anyway?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about cocoa