views:

87

answers:

2
+1  Q: 

Where to #include?

In my past applications I have been #importing into my *.h files where needed. I have not really thought much about this before as I have not had any problems, but today I spotted something that got me to thinking that maybe I should be #import-ing into my .m files and using @class where needed in the headers (.h) Can anyone shine any light on the way its supposed to be done or best practice?

gary

+4  A: 

In any source file, import only what you need to make that individual file valid for compilation. @class is also preferable to importing another class's headers, because the less you load the less you compile.

NSResponder
Thank you, I understand now.
fuzzygoat
+3  A: 

As a rule of thumb it is fine to use @class in your header file and an #import in your .m files. You'll get an error if you do it wrong from the compiler :)

Basically, if you are only making reference to the class you want to use, but not any specifics of the class then @class is all that is required. It tells the compiler "I'm going to be using this class here - you don't need to know much about it, other than that it is valid". (The compiler then knows to reserve a pointer for it).

If you were going to be referencing any properties/methods within the class, the compiler would start complaining (as it wouldn't have enough information about the class) so in those cases it wants you to import the file (#import xxx) in order to provide the class specifics to the compiler.

Hope this helps

davbryn