In Objective-C, importing same headers in every class make compile time longer?

前端 未结 5 415
轻奢々
轻奢々 2021-01-11 17:23

I\'m a beginner of Objective-C/iOS programing.

I want make a one header file which includes all class headers I use in my project.
And import the header in every

相关标签:
5条回答
  • 2021-01-11 17:42

    In general, newly-generated iOS projects come with this functionality, which is called a precompiled header or prefix header, and is a file that has the extension .pch.

    You can throw all the headers you want in there and Xcode will pre-compile it before it builds anything else, and use it to compile the other compilation units in your project (e.g. .m files).

    Using a precompiled header may or may not increase compile time; in general, it reduces compile time, as long as you have a lot of common headers and/or a lot of source files.

    However, it's not necessarily good practice to treat the pre-compiled header like a big dumping ground, as your compilation units can form implicit dependencies on all sorts of stuff when you may want to enforce loose coupling between components.

    0 讨论(0)
  • 2021-01-11 17:54

    If you want headers imported globally you should do so in the YourProject-Prefix.pch file. It should look something like this..

    #import <Availability.h>
    
    #ifndef __IPHONE_4_0
    #warning "This project uses features only available in iOS SDK 4.0 and later."
    #endif
    
       #ifdef __OBJC__
       #import <UIKit/UIKit.h>
       #import <Foundation/Foundation.h>
       #import "YourGlobalHeader.h"
    #endif
    

    Now, All of your classes have YourGlobalHeader.h automagically imported.

    0 讨论(0)
  • 2021-01-11 17:56

    You can import that header file in your projects prefix_pch file.then You can use it in Your classes .

    0 讨论(0)
  • 2021-01-11 18:02

    The issue with all the classes in one header is that every time you change a class header then all the files including it even indirectly will need to be recompiled, whilst if you only import the needed class and also use @class when you can then only the files that directly use the class need to be recompiled. Thus in the first case there will be many more compilations than in the latter. This is the way I would recommend to start.

    However when your code becomes more stable and classes do NOT change then putting them all in one header can improve the compile time as the precompiled header will contain the same information for each file. What I would do is when the code is not changing so much is put the mature classes into a Framework and the Framework header will include all these classes.

    0 讨论(0)
  • 2021-01-11 18:03

    Putting all the headers in one file may improve build performance in certain cases, but you probably won't notice the difference.

    It is better to keep your class headers in different files for purposes of organization. Also, if you are only including the headers that you need in your source files then your build time will be reduced, although again not noticeably if you are using a decent build machine.

    0 讨论(0)
提交回复
热议问题