ARC issue -- difference between @class and #import


#1

Hi,

I was going through Chapter 8 / Challenge 2 and was running into a problem with ARC.
When I use @class Person in the RMDocument.h file I’m getting an error message from
the ARC “Receiver ‘Person’ for class message is a forward declaration”. Well, I just changed
to #import “Person.h” and it is was working. So in general, when to use @class and when to
use #import ? I’ve also seen that’s is a common practice to add both lines. What
would be your advice ?


#2

@class Person;” tells the compiler, “This class called Person exists,” so it doesn’t complain about it when you use it as a type for instance variables, or as an argument to a method. However, it’s not enough information to be able to call methods on the class, thus we need to do the #import.

The pattern we recommend is to use @class in header files, and then do any necessary #import-ing in the implementation files. The reason is that if you #import everything in header files, as your app grows it can cause chicken-and-egg problems. Using @class in header files instead solves this problem.

Incidentally, this issue has nothing to do with ARC, but for some reason Xcode presents a lot of errors as if they were ARC issues. Presumably that will be resolved in a future update.

Adam