When to include the #import in the implementation file
Learning Objective-C and oth开发者_开发知识库er c based Languages I learned that you should put the #includes and the #imports in the header file. And the @class goes there as well. Recently looking at example code from apple and other sources around the web the @class is in the header and all the imports are in the implementation file.
Which is correct? Are there reasons for both? Also why do you need to provide the @class declaration if you are importing the header file.
Neither case is "more correct", there are definitely reasons for both behaviours. For example, think about the case where you have two classes, each which has a reference to an object of the other type:
ClassA.h:
@interface ClassA : NSObject
{
ClassB *b;
}
ClassB.h:
@interface ClassB : NSObject
{
ClassA *a;
}
This code won't compile - you have a circular dependency in these headers. The solution is to forward declare the required classes using the @class
directive.
A situation where you might prefer the #import
directive in the header file might be if you have some common code besides just a class name that you care about in the other header - maybe C style functions or enumerated types or something.
Learning Objective-C and other c based Languages I learned that you should put the #includes and the #imports in the header file.
no - not in c based languages. you should put them in the implementation files where possible. you can't always create a zero-dependency header, but you should minimize it.
c based languages take a long time to compile, especially when the dependencies and includes are very complex, or there are unnecesary includes (introducing more dependency, coincidentally).
And the @class goes there as well. Recently looking at example code from apple and other sources around the web the @class is in the header and all the imports are in the implementation file. Which is correct?
use forward declarations (@class NAME;
, @protocol NAME;
, struct NAME
, class NAME;`, etc.) wherever you can.
Are there reasons for both?
including in the header is the lazy way, it slows down your build times and introduces a lot of dependency. it's convenient because you don't have to write as many include/import declarations, but it's not considerate for people who must use your programs.
Also why do you need to provide the @class declaration if you are importing the header file.
if the class interface is already visible (has been included already, or another file has declared it), you do not need both . you'll need the interface visible if you intend to use the type (apart from some very trivial cases).
you certainly won't be excited to correct the mistake once your build times have grown slow -- it's best to learn and implement using the right approach. if you've not developed a complex c project, then you'd likely be very surprised to learn how much time is lost while compiling.
if you expect that your programs will never become nontrivial, and will be never shared or reused, then do whichever you prefer.
good luck!
精彩评论