1

My problem is this:

  • My framework contains public and private headers - the public headers import private headers in the framework
  • My app that links against this framework imports public headers

Now when I compile it, Xcode complains about missing files (the private headers that are indirectly imported via the frameworks public headers). I read somewhere on stackoverflow that I should do this:

"In the public header file use @class to include other interfaces and use #import in the implementation file (.m)."

I find this solution pretty unsatisfying - you have to use it for circular dependencies, too. Is there any better way to keep my headers private?

Cœur
  • 37,241
  • 25
  • 195
  • 267
jhoff
  • 100
  • 1
  • 10
  • 1
    It's not clear exactly what you are trying to accomplish. What is "unsatisfying" about the forward-declaration approach? – Kristopher Johnson Apr 27 '10 at 14:51
  • I find it unsatisfying that I have to differentiate between public and private headers in the source itself, instead of just being able to set it in the target configuration. If I move a private header to public later, I have to "clean up" the code afterwards - which may lead to "out-of-sync" issues. – jhoff Apr 29 '10 at 08:06

1 Answers1

1

To get about circular references use the @class directive in the header and the #import in the source file.

In OtherClass.h:

@class MyClass;
@interface OtherClass
{
    MyClass *myInstance;
}
@end

In OtherClass.m:

#import "OtherClass.h"
#import "MyClass.h"
@implement OtherClass
@end

In MyClass.h:

@class OtherClass;
@interface MyClass
{
    OtherClass *otherInstance;
}
@end

In MyClass.m:

#import "MyClass.h"
#import "OtherClass.h"
@implement MyClass
@end
  • Thanks for the elaborate example, but this @class declaration is exactly what I find unsatisfying (see comment above) – jhoff Apr 29 '10 at 08:07
  • Don't get me wrong, this is a good and valid solution, but I was asking for a "cleaner" solution than this - or if there is any :) – jhoff Apr 29 '10 at 08:16
  • @class is as clean as it gets - in fact that keyword is used mainly to prevent or resolve circular import situations. Same with "class" in C++. – CodeSmile Oct 07 '13 at 16:47