开发者

Where to #import on Objective-C

My project has been increasing in size and I'm a little confused about where should I #import header files.

There are 3 main locations where I can import headers:

  1. The .pch file (prefix)
  2. The .h file (header)
  3. the .m file (implementation)

I don't care if the compiler takes more time to compile the files, all I care is that the end product is as fast as possible and uses the least amount of memory.

So, my questions are:

  1. If most files need a specific header, is it ok to add it to the .pch file, or is it more efficient to add it to just the required files?
  2. Should imports be done in the .h or .m file? I know I must add it to the .h file if i'm going to declare it there, but if I don't need to declare it in the .h file is there a problem of leaving the impor开发者_JAVA百科t there?


  1. No, it is not ok to include it into the .pch file. This file is precompiled to every module in the project. Read about it here.

  2. Read this question and answer.


Put your imports in your .m whenever you can. If you are using a class in your .h use @class to forward the declaration, then #import in your .m. The only time you should import in your .h are protocols that you implement or superclasses.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜