很多刚开始学习iOS开发的同学可能在看别人的代码的时候会发现有部分#import操作写在m文件中,而h文件仅仅使用@class进行声明,不禁纳闷起来,为什么不直接把#import放到h文件中呢?
这是因为h文件在修改后,所有import该h文件的所有文件必须重 新build,因此,如果把#import写在h文件中,import该h文件的文件也就会产生不必要的编译,增加编译时间,特别是在项目文件多的情况 下。想象一下,如果只是修改一个h文件而导致上百个文件不必要的编译,那是一件多么让人纠结的事情。。。
对于@class只是告诉编译器有这个class,请不要报错或警告,因此不会给编译造成影响。
什么时候用@class这种方式声明比#import好呢?
stackoverflow上的高手们给了不少建议:
Randy Marsh:
When I develop, I have only three things in mind that never cause me any problems.
- Import super classes
- Import parent classes (when you have children and parents)
- Import classes outside your project (like in frameworks and libraries)
For all other classes (subclasses and child classes in my project self), I declare them via forward-class.
Justin:
Simple answer: You
Here are some common examples of physical dependence:
- Any C or C++ value (a pointer or reference is not a physical dependency). If you have aCGPoint
as an ivar or property, the compiler will need to see the declaration ofCGPoint . - Your superclass.
- A method you use.
下面来说一下#import同class之间的区别
2.在.h头文件中进行声明时用#import的话,如果100个头文件都#import同一个头件,或者这些文件是依次引用的,如A->B,B->C,C->D,当最开始的那个头文件有变化后进行编译时,后面所有引用它的类都需要重新编译,如果引用最开始的头文件的类很多的话,那么这将耗费大量的时间,而用@class则不会,可能有人会想即然.h只是用@class只是简单的一个声明告编译器有这个类不让其报错,那么.m中要用到引入的类的方法和属性时,不还是要#import头文件一次,是的这个是对的,但编译器编译的时候只编译头文件的,所以你的.m中用#import与编译时间没太大关系
3.接下来说说什么时候该用@class,什么时候该用#import进行声明,
3)如果有代理的话必须用#import 如
#import <UIKit/UIKit.h>
#import "TableViewWithBlock.h"
#import "ASIHTTPRequest.h"
#import "ListTableView.h"
@interface QPLSetUserInfoViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,ASIHTTPRequestDelegate,ListViewDelegate>
@end
如果#import "ListTableView.h" 改为@class 的话会报错