Hello,
I'm a beginner Objective-C programmer, and I've just run into a problem when developing my first major project.
My program consists of several objects, arranged in a heirarchy (although not inheriting from one another).
Class A --> Class B --> Class C
Objects of Class A own several objects of Class B, which in turn own several objects of Class C.
However, Class B objects also contain a pointer to the Class A object that owns them, and Class C objects contain a pointer to the Class B object that owns them.
I had put the following lines in ClassA.h:
#import "ClassB.h"
ClassB.h:
#import "ClassA.h"
#import "ClassC.h"
and ClassC.h:
#import "ClassB.h"
However, Xcode was giving me parse errors for my definition of a ClassB pointer in ClassC.h.
I eventually realised that, because I'm chaining together #import statements and the compiler basically sticks it all together, ClassA.h actually contains ClassC, then ClassB, then ClassA, in that order, which of course means that ClassC is trying to reference ClassB before it has been defined.
The only way I could find to get around this was to add an "@class ClassB" line to ClassC.h, and a "@class ClassA" line to "ClassB.h", instead of the #import "ClassB.h" and #import "ClassA.h" statements.
My question (after such a long-winded run up) is whether this is the correct procedure? Am I missing something? Steve Kochan's Objective-C book doesn't mention this issue, so I'm not sure what I should do in the future. Surely if at some point my Class C objects needed to reference actual instance methods from Class B, rather than just defining pointers, the @class directive wouldn't be enough?
Any advice would be appreciated!
Thanks.
I'm a beginner Objective-C programmer, and I've just run into a problem when developing my first major project.
My program consists of several objects, arranged in a heirarchy (although not inheriting from one another).
Class A --> Class B --> Class C
Objects of Class A own several objects of Class B, which in turn own several objects of Class C.
However, Class B objects also contain a pointer to the Class A object that owns them, and Class C objects contain a pointer to the Class B object that owns them.
I had put the following lines in ClassA.h:
#import "ClassB.h"
ClassB.h:
#import "ClassA.h"
#import "ClassC.h"
and ClassC.h:
#import "ClassB.h"
However, Xcode was giving me parse errors for my definition of a ClassB pointer in ClassC.h.
I eventually realised that, because I'm chaining together #import statements and the compiler basically sticks it all together, ClassA.h actually contains ClassC, then ClassB, then ClassA, in that order, which of course means that ClassC is trying to reference ClassB before it has been defined.
The only way I could find to get around this was to add an "@class ClassB" line to ClassC.h, and a "@class ClassA" line to "ClassB.h", instead of the #import "ClassB.h" and #import "ClassA.h" statements.
My question (after such a long-winded run up) is whether this is the correct procedure? Am I missing something? Steve Kochan's Objective-C book doesn't mention this issue, so I'm not sure what I should do in the future. Surely if at some point my Class C objects needed to reference actual instance methods from Class B, rather than just defining pointers, the @class directive wouldn't be enough?
Any advice would be appreciated!
Thanks.