Difference between @ class and # import in objective-C

Source: Internet
Author: User

# Include is used in C/C ++ to include the header file. The disadvantage is that the same header file may be contained multiple times.
# Import is used in OC to include the header file. The advantage is that the same header file can only be contained once.


@ ClassAnd# ImportIt is to solve the deadlock problem caused by loop reference.

# Import will contain all information about this class, including entity variables and methods, while @ class only tells the compiler that the name declared after it is the name of the class, as for how this class is defined, you do not need to consider it for the moment. We will tell you later.

In the class declaration. generally, you only need to know the name of the referenced class in the H file, and do not need to know its internal entity variables and methods. in the H file, @ class is generally used to declare that this name is the name of the class. But in the implementation class. Because the internal object variables and methods of the referenced class are used, you need to use # import to include the header file of the referenced class.

If there is a circular dependency such as a-> B, B-> A, if # import is used to contain each other, a compilation error occurs, if @ class is used to declare each other in the header files of the two classes, no compilation error occurs.

So in general, @ class is placed in the. h file, just to reference this class in. h and reference this class as a type. In the. M file that implements this. h file, if you need to reference the object variables or methods of this class, use # import.

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.