Difference between # import and @ class in objective-C

Source: Internet
Author: User
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. This header file includes all the definitions of this class. It
Knowing that you have defined a price, but there is no specific information except the class introduced by @ class.

# Import <> what is the difference with # import? "" Indicates a self-created file, which will be first searched in the current directory. If it cannot be found, it will be searched elsewhere, "" And <> are relative to the files created by the user, and the files created by the system.

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 header files, you generally only need to know the name of the referenced class, and do not need to know its internal entity variables and methods, therefore, in the header file, @ class is generally used to declare that this name is the name of the class. In the implementation part of the class, the internal entity variables and methods of this reference class are used, therefore, 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 need to be re-compiled. If there are many classes, this will take a lot of time. Instead, @ class is not used.

4. 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.


Therefore, in general, @ class is placed in the interface, just to reference this class in the interface and use this class as a type. In the implementation class that implements this interface, if you need to reference the object variables or methods of this class (there is an inheritance relationship or proxy ), you still need to import the classes declared in @ class.
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.