The difference between IOS Import,include and class

Source: Internet
Author: User

#import Determine that a file can only be imported once, so that you do not have a problem with recursive inclusion

. Import contains all the information for this class, including entity variables and methods (in. h files)

You still need to use #import in M files

In general, if there is an inheritance relationship #import

#import The advantage of #include is that it does not cause cross-compilation,

In General, #import When importing header files for Objective C,

2. You can #import in the implementation file , not #import in the header file .


A #include is used when including a C + + header file.

@class Just tell the compiler that the name that is declared after it is the name of the class, and how these classes are defined, will tell you later.

2.   In a header file, you typically just need to know the name of the referenced class. There is no need to know the entity variables and methods inside of it, so @class to declare that the name is the name of the class. In the implementation class, it is necessary to use

#import is to go through the header file of the referenced class, that is, the variables and methods in the . h file are included once, and only once, and @class not used, so the latter is more efficient in compiling.

3. in terms of compilation efficiency , when the first header file changes, all subsequent classes that reference it need to be recompiled, and if your class has a lot of words, it will not be used @class .

4. If you have circular dependencies, if you use #import to include each other, then a compilation error occurs, and if you use @class in the header files of two classes to declare each other, there will be no compilation errors.

3. Can @class in the header file + #import in the implementation file , it is not #import in the header file .

#include: It is used to reference the header file that comes with the system, and the compiler looks for the file in the System files directory.

#include "x.h": user-defined files are quoted in double quotes, the compiler first looks in the user directory, then looks in the installation directory, and finally looks in the system files.

Pay attention to handling duplicate references when using #include

@class

       is primarily used to declare a class, telling the compiler that the name behind it is the name of a class,There is this kind of existence,The implementation of this class is not known for the time being, so the implementation file in the following is needed to#importThis class, at this time, contains all the information of the referenced class,But how the class is implemented without telling the compiler.if. MThe file is used for this class, or to. MFile SummaryImportof this class。

#include and #import will include all the information about this class when referencing a class, including variable methods, but this will affect the efficiency of the compilation. .   

Referencing a class in interface , using @class, it will use this class as a type, and in implementing this interface file, if you need to refer to the entity variables or methods of this class. , you still need to import This class declared in the @class .

#include <>: for references to system files, the compiler looks for the file in the System files directory.

#include "xx.h": for a user-defined file reference, the compiler will first go to the user directory to find, and then go to the installation directory, and finally to the system directory lookup.  

@class

(2) In addition, if there is a cyclic dependency, if you declare each other in the header file of the two files with #import , then there will be errors in the header file recycling, then the @class declaration in the header file will not be error;

(3) There is also the time to customize the proxy, if you want to declare the proxy in the header file, such as @interface secondviewcontroller:uiviewcontroller application #import Otherwise there will be an error, notethat the XXXX Delegate is custom.

Pre-compilation Directives

#import由gcc编译器支持

In Objective-c, #import is used as an improved version of the #include directive.

@class is typically used when you need to declare an instance variable of the class in a header file,

Just @class ClassB that there is no include ClassB.h. Only need to #import in the CLASSA.M file that requires ClassB ClassB.h

If you only need to declare a CLASSB pointer in ClassA.h, you can declare it in ClassA.h

@ClassB

ClassB *pointer;

In objective-c ,#import is used as an improved version of the #include directive. In addition,#import determine that a file can only be imported once, which allows you to have no problem with recursive inclusion.

#import classb.h  #include whole . H header file. If there are many . M file #import classa.h then these files will also #import classb.h >" and added unnecessary # Import. H Include > It's very important.

just @class classb  There is no include Classb.h classb Classa.m file   #import ClassB.h

if ClassA.h only need to declare a pointer to a ClassB, you can declare it in ClassA.h @ClassB


This article is from the "11204872" blog, please be sure to keep this source http://11214872.blog.51cto.com/11204872/1754490

The difference between IOS Import,include and class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.