# Import, # difference between include and @ Class

Source: Internet
Author: User

1.
# Include is the keyword used to reference files in C, while # import is the keyword used to replace include in obj-C. # Import can ensure that the same file can be imported only once, thus avoiding the repeated reference problem that is easily caused by # include. That is, classa references classc and classb also references classc, when both classa and classb are referenced by classd, a duplicate reference error is reported.

2.
# Import "" And # import <>:# import "to find the file to be imported from the current working directory. If the file is not found in the system class library, # import <> directly searches for the file to be imported from the system library.

3.
# Import and @ class:

@ Class only tells the compiler that this name is a class name. You do not need to consider how this class is implemented. Introduction @ class is mainly used to solve the reference deadlock-if the two classes have a circular dependency, that is, a-> B, B-> A, if # import is used for mutual inclusion, A compilation error occurs:
Expected specifier-qualifier-list before 'A' or expected specifier-qualifier-list before 'B '.

In general. in the H file, you only need to know the class name, so use @ class, and in. in M files, you usually need to know the member variables of the class, that is, the method. Therefore, you must use # import to import the class files.

Why not use # import to import class files directly in the. h file, because if a large number of header files are imported, the compiler will spend a lot of time compiling.

# Import needs to be used in the. h file:
1/If there is an inheritance relationship, use # import. For example, if a inherits B, you need to import B in.
2. To use a class with category, use # import in the. h file to import the category of this 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.