IOS development technology-header file reference (@ class/# import/# include)

Source: Internet
Author: User

I. Differences between # import and # include in Objective-C

Pre-compiled commands
Objective-C: # import
C, C ++: # include
# Import supported by the gcc compiler

In Objective-C, # import is used as an improved version of the # include command. In addition, # import determines that a file can only be imported once, so that you will not encounter problems in recursive inclusion.

It is up to you to decide which one to use. Generally, # import is used when the Objective-C header file is imported, and # include is used when the C header file is included. For example:

# Import <foundation/Foundation. h>

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

# Compared with # include, import does not cause cross-compilation.
2. @ class is used for class reference
@ Class is to tell the compiler that there is such a class. What is the definition of the class?
@ Class is generally used when the header file needs to declare an instance variable of this class. In the m file, you still need to use # import

For example:
In ClassA. h
# Import ClassB. h is equivalent to # include the entire. h header file. If there are many. m Files # import ClassA. h, these files will also # import ClassB. h added unnecessary # import during compilation, wasting Compilation Time. In large software, it is very important to reduce the include in. H files.

If
Only @ class ClassB does not include ClassB. h. You only need to # import ClassB. h In the ClassA. m file that requires ClassB

So when can I use @ class?
If you only need to declare a ClassB pointer in ClassA. h, you can declare it in ClassA. h.
@ ClassB
...
ClassB * pointer;

 


From andy Pan's column

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.