My inquiry is this. When does one use #import and when does one use @class?
Simple answer: You #import
or #include
when there is a physical dependency. Otherwise, you use forward declarations (@class MONClass
, struct MONStruct
, @protocol MONProtocol
).
Here are some common examples of physical dependence:
- Any C or C++ value (a pointer or reference is not a physical dependency). If you have a
CGPoint
as an ivar or property, the compiler will need to see the declaration of CGPoint
.
- Your superclass.
- A method you use.
Sometimes if I use a @class declaration, I see a common compiler warning such as the following:
"warning: receiver 'FooController' is a forward class and corresponding @interface may not exist."
The compiler's actually very lenient in this regard. It will drop hints (such as the one above), but you can trash your stack easily if you ignore them and don't #import
properly. Although it should (IMO), the compiler does not enforce this. In ARC, the compiler is more strict because it is responsible for reference counting. What happens is the compiler falls back on a default when it encounters an unknown method which you call. Every return value and parameter is assumed to be id
. Thus, you ought to eradicate every warning from your codebases because this should be considered physical dependence. This is analogous to calling a C function which is not declared. With C, parameters are assumed to be int
.
The reason you would favor forward declarations is that you can reduce your build times by factors because there is minimal dependence. With forward declarations, the compiler sees there is a name, and can correctly parse and compile the program without seeing the class declaration or all of its dependencies when there is no physical dependency. Clean builds take less time. Incremental builds take less time. Sure, you will end up spending a little more time making sure the all the headers you need are visible to every translation as a consequence, but this pays off in reduced build times quickly (assuming your project is not tiny).
If you use #import
or #include
instead, you're throwing a lot more work at the compiler than is necessary. You're also introducing complex header dependencies. You can liken this to a brute-force algorithm. When you #import
, you're dragging in tons of unnecessary information, which requires a lot of memory, disk I/O, and CPU to parse and compile the sources.
ObjC is pretty close to ideal for a C based language with regards to dependency because NSObject
types are never values -- NSObject
types are always reference counted pointers. So you can get away with incredibly fast compile times if you structure your program's dependencies appropriately and forward where possible because there is very little physical dependence required. You can also declare properties in the class extensions to further minimize dependence. That's a huge bonus for large systems -- you would know the difference it makes if you have ever developed a large C++ codebase.
Therefore, my recommendation is to use forwards where possible, and then to #import
where there is physical dependence. If you see the warning or another which implies physical dependence -- fix them all. The fix is to #import
in your implementation file.
As you build libraries, you will likely classify some interfaces as a group, in which case you would #import
that library where physical dependence is introduced (e.g. #import <AppKit/AppKit.h>
). This can introduce dependence, but the library maintainers can often handle the physical dependencies for you as needed -- if they introduce a feature, they can minimize the impact it has on your builds.