ios中@class和 #import
来源:互联网 发布:姿态融合算法 编辑:程序博客网 时间:2024/05/29 03:09
很多刚开始学习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 thatnever cause me any problems.
Import super classes
Import parent classes (when you have children andparents)
Import classes outside your project (like inframeworks and libraries)
For all other classes (subclasses and child classes inmy project self), I declare them via forward-class.
Justin:
Simple answer: You #import or #include when there is aphysical dependency. Otherwise, you use forward declarations (@class MONClass,struct MONStruct , @protocol MONProtocol ).
Here are some common examples of physical dependence:
Any C or C++ value (a pointer or reference is not aphysical dependency). If you have aCGPoint as an ivar or property, the compilerwill need to see the declaration ofCGPoint .
Your superclass.
A method you use.
最后,我建议还是养成良好的import习惯,不要偷懒都把import放在h文件中,无论参与的项目大小,养成良好的编程习惯非常重要。
- ios中@class和 #import
- IOS中#import,#include和@class
- ios中@class和 #import区别
- iOS中#import和@class的区别
- iOS中#import和@class的区别
- iOS中#import和@class的区别
- ios-@class和#import
- iOS #import和@class 区别
- iOS @class 和#import“”引用
- iOS#import和@class区别
- iOS之@class和#import
- ios中@class和 #import,两种方式的讨论
- ios中@class和 #import,两种方式的讨论
- ios中@class和 #import,两种方式的讨论
- ios中@class和 #import,两种方式的讨论
- iOS#import和#include和@class用法
- ios中#import与@class的区别
- iOS中的#import和class区别
- LaTex常见错误对照表及原因解析
- Create a simple unit test project with Visual Studio Unit Testing Framework
- android常见问题二:真机测试无法访问本机服务器端
- 编辑框的基本美化
- poj A Knight's Journey
- ios中@class和 #import
- 使用pull解析器读取xml文件
- ISP DSP diff
- 如何在linux下运行python程序
- eclipse中使用weblogic server920_win32.exe
- 取得程序当前的EIP地址(x86)
- fread 和fwrite操作浮点数的一个问题(linux系统)
- C++中的快排
- Spring jdbcTemplete datasource