OC import class # differences between import and @ Class

Source: Internet
Author: User

Difference between # import and @ class in objective-C

In objective-C, you can use # import and @ class to reference other types. But do you know the differences between the two?

@ Class is called forward-class. You often see the reference through @ Class in the header file definition, because when you only use @ class to introduce a class, the compiler knows that there is such a class, that is, it can recognize engine * engine;

 

In the implementation file, if you want to access the engine. price, the compiler will go wrong, even if you use @ class to introduce it. in this case, we need to use # import to introduce the engine. h: The header file, which includes all the definitions of this class. It knows that you have a price definition, and there is no specific information except the class introduced by @ class.

 

More detailed analysis:

 

In objective-C, when a class needs to reference the interface of another class, the pointer of the referenced class needs to be created in the Class header file.

Person. h @ interface person: nsobject {woman*WOMAN; man*Man ;}.......

 

If you compile it directly, an error occurs because you do not know what woman and man are.

At this time, there are two options: one is import the header files of the two referenced classes, and the other is to use @ class to declare that woman and man are class names. The difference between the two is as follows:

1. 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 these classes are defined, don't worry about it for the moment. I will tell you later;

2. In the header file, you generally only need to know the name of the referenced class, and do not need to know its internal entity variables and methods.In the header fileGenerally, @ class is used to declare that this name is the name of the class, whileIn the implementation part of the classBecause 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.

3. in terms of compilation efficiency, if you have 100 header files # imported the same header file, or these files are referenced in sequence, such as a-> B, B-> C, c-> D. When the header file at the beginning changes, all the classes that reference it later need to be re-compiled. If there are many classesTime-consuming. Instead, @ class is not used.

4. IfCircular dependency, such as a-> B, B->., IfIf # import is used for mutual inclusion, a compilation error occurs., IfIf @ class is used to declare each other in the header files of the two classes, no compilation error occurs.

Therefore, in general, @ Class is placed in the interface, just to reference this class in the interface, use this class as a Type. InImplementation classIf you need to reference the object variables or methods of this classThe class to be declared by the import in @ 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.