tags:

views:

227

answers:

3

I'm new to objective-c and would like to know the best practice for importing some external headers that I use in my class.

Should I be storing the #import "classB.h" in my own classes .h file or in the .m file?

What's the difference?

Thanks!

+4  A: 

It is proper practice to put a forward class declaration (@class classB;) in the header and #import "classB.h in the .m

A forward class declaration, like @class classB; lets the compiler know it should expect the class later on, and it shouldn't complain about it at the moment.

Ryan Neufeld
I think you mean "in the .m" in your first sentence.
Paul Tomblin
Thanks for pointing that out.
Ryan Neufeld
+2  A: 

To the compiler, it really doesn't matter. You could just throw forward declarations in your .h and then wait to #import until your .m file. See this post on SO for more info on this.

From a clean-code prospective, some may argue that putting the imports in your implementation file keeps the details closer to where they are needed (see that link above as well; the people there reference this idea).

Marc W
+2  A: 

To avoid circular references, only #import a header file in another class's header file if it's inheriting from that class. Otherwise, use @class ClassName to declare the class type if you need it in your header file, and #import it in the implementation file.

Marc Charbonneau
It is just worth mentioning that you can't get circular references with the #import directive.
Jason Coco