The difference between----#import, #include和 @class of iOS development

Source: Internet
Author: User

1. In general, when importing header files for Objective C, use #import, which includes C + + header files, with # include.

2. #import determine that a file can only be imported once, so that you do not have problems with recursive inclusion. < tags >

Therefore, the benefit of #import比起 # include is that it does not cause cross-compilation.

#import && #class:

1. Import will contain all the information for this class, including entity variables and methods (in. h files), and @class just tells the compiler that the name that is declared after it is the name of the class and how these classes are defined will be told later.

2. In the header file, it is generally only necessary to know the name of the referenced class. There is no need to know the entity variables and methods inside, so it is common to use @class in the header file to declare that the name is the name of the class. In the implementation class, you need to use #import to include the header file of the referenced class, because it will use the internal entity variables and methods of the referenced class.

Note: #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 is not used, so the latter is more efficient in compiling.

3. In terms of compilation efficiency, if you have 100 header files that #import the same head file, or if the files are referenced in turn, such as A–>b, B–>c, C–>d, and so on. When the first header file changes, all the subsequent classes that reference it need to be recompiled, which can take a lot of time if you have a lot of classes. Instead of using @class, you don't.

4. If there are cyclic dependencies, such as A–>b, b–>a such interdependencies, if the use of #import to each other, then there will be a compilation error, if you use @class in the header files of two classes to declare each other, there will be no compile errors appear.

Note: Practice has proved that a, B mutual #import does not appear compile errors. This article is not valid because the file is only imported once when the < tag > has been explained #import.

Summarize:

1. If it is not C + +, try to use #import.

2. Can be #import in the implementation file, it is not #import in the header file.

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

The difference between----#import, #include和 @class of iOS development

Related Article

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.