Declaring two classes that contain instance variables of each others' types.
- by Benji XVI
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?