zoukankan      html  css  js  c++  java
  • ios中@class和 #import,两种方式的讨论

    刚开始学习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.

    1. Import super classes
    2. Import parent classes (when you have children and parents)
    3. 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 #import or #include when there is a physical 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 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习惯,不要偷懒都把import放在h文件中,无论参与的项目大小,养成良好的编程习惯非常重要。

    更多阅读请访问http://www.hopean.com

    原文地址:http://blog.csdn.net/winsdom123456/article/details/7608025

  • 相关阅读:
    使用java Graphics 绘图工具生成顺丰快递电子面单
    NPM使用命令总结
    MYSQL主从库同步配置过程
    Redis的事务功能详解
    MapReduce 原理与 Python 实践
    Django权限机制的实现
    Python调用外部程序——os.system()和subprocess.call
    oracle11g安装教程(注意事项及图文教程)
    顶级的JavaScript框架、库、工具及其使用
    经典CSS坑:如何完美实现垂直水平居中?
  • 原文地址:https://www.cnblogs.com/hopeanCom/p/2789560.html
Copyright © 2011-2022 走看看