Possible Duplicate:
How can I have references between two classes in Objective-C?
Objective-C #import loop
I’m getting a couple errors in my code and I’m not sure but I think its because I’m #importing an interface inside another interface where I’m #importing the other interface. If I’m confusing you I’ll give you an example.
#import "OneClass.h"
@interface SecondClass : NSObject
{
OneClass * obj;
}
#import "SecondClass.h"
@interface OneClass : NSObject
{
SecondClass * obj;
}
Yes, you have a circular import. The problem here is that the second import (the one that re-imports your first header) is basically ignored by the compiler, since it thinks it’s already imported that header.
The solution here is to use
@classforward-declarations instead of using#imports. Not only does this solver the circular import problem, but it’s a better idea anyway since it breaks unnecessary dependency chains (e.g. if I edit OneClass.h, SecondClass.h won’t need to be re-processed).To apply this here, simply remove the
#import OneClass.hin SecondClass.h and replace it with@class OneClass;In the more general case, you don’t ever need to
#importa header file just to declare an ivar/property/method that uses a class from that header. The@classtoken is sufficient. You do, however, need to#importthe header file if you’re inheriting from the class, or if you’re referencing another non-class type declared in that header. Also remember that if you use@classin your header, you need to remember to put the actual#importinto your .m file.