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!
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!
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.
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).
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.