• #import 与 #include的区别,以及 @class


    #import

    In the C language, the #include pre-compile directive always causes a file's contents to be inserted into the source at that point. Objective-C has the equivalent #import directive except each file is included only once per compilation unit, obviating(去除) the need for include guards.

    for example 三个文件

    文件A.h 

    文件B.h

    文件C.h

    文件C.h需要引入A.h、B.h  ,文件B.h需要引入文件A.h,这样就重复引用了A.h两次,使用#import可以进行优化

    一、 Objective-C 中 #import 和 #include 的区别   




    预编译指令
    Objective-C:#import
    C,
    C++:#include
    #import由gcc编译器支持

    在 Objective-C 中,#import 被当成 #include 指令的改良版本来
    使用。除此之外,#import 确定一个文件只能被导入一次,这使你在递归包含中不会出现问题

    使用哪一个还是由你来决定。一般来说,在导入 Objective-C 头文件的时候使用 #import,包含 C 头文件时使用 #include。比如:

    #import <foundation /Foundation.h>

    #include <asl .h>
    #include <mach /mach.h>



    #import比起#include的好处就是不会引起交叉编译

    二、@class是用来做类引用的
    @class就是告诉编译器有这么一个类,至于类的定义是啥不知道

    @class一般用于头文件中需要声明该类的某个实例变量的时候用到,在m文件中还是需要使用#import



    举个例子说明:
    在ClassA.h中
    #import ClassB.h 相当于#include整个.h头文件。如果有很多.m文件#import ClassA.h,那么编译的时候这些文件也会#import ClassB.h增加了没必要的#import,浪费编译时间。在大型软件中,减少.h文件中的include是非常重要的。

    如果
    只是@class ClassB 那就没有include ClassB.h。仅需要在需要用到ClassB的ClassA.m文件中 #import ClassB.h

    那么什么时候可以用@class呢?
    如果ClassA.h中仅需要声明一个ClassB的指针,那么就可以在ClassA.h中声明
    @ClassB
    ...
    ClassB *pointer;
  • 相关阅读:
    Ant 中作用代理
    linux通用自动清理日志脚本
    linux shell 脚本攻略 下
    我在写shell自动部署脚本碰到的问题
    linux shell 脚本攻略(上)
    Java Object.wait() jvm的bug
    shell 脚本 更新或者添加host ,并且增加hostname映射到hosts (修改)
    记一次子域名IP搜集reconngkali
    ie6下面试用png使用滤镜需知
    canvas 使用 图片 切片的时候 在chrome 下 要注意的 一点
  • 原文地址:https://www.cnblogs.com/easonoutlook/p/2642815.html
Copyright © 2020-2023  润新知