Say class A depends on Class B and via versa.
So obviously in a.h I would put #import”b.h” and in “b.h” I would import “a.h”
After that
Should we
declare @class A
in b.h AND @class B in a.h
Should we do both or should we use @class only once?
Why?
I reasoned that if I do it only once, say including @class B in a.h, given that a.h also contains #import “b.h” then the class a.h already knows about b.h
However it seems that I got to do it both in xcode and I wonder why.
There’s nothing wrong with using a forward declaration (i.e.
@class Foo) in the header file for each class. You can do that if all you need is for each class to reference the other. A forward declaration just tells the compiler “This name refers to a class, and the actual declaration of the class will come later.”As you say, you could also use a forward declaration of B in A.h, and then just import A.h in B.h. I don’t think there’s any real benefit to doing that unless there are other things in A.h that you also need to reference in B.h.
If you mean that you’re getting an error when you import A.h in B.h instead of using a forward declaration of A, please post the error. I don’t think there should be a problem with that. (And in any case, the issue would be a compiler issue or an Objective-C issue, not an Xcode problem. Xcode is just the IDE.)