[Reading Notes] iOS-header file import-@ class notes, ios -- @ class

Source: Internet
Author: User

[Reading Notes] iOS-header file import-@ class notes, ios -- @ class

I. There are two different ways to import a header file: use quotation marks or angle brackets, for example, # import <Cocoa/Cocoa. h> and # import "Tire. h ". the statements with Angle brackets are used to import system header files, while the statements with quotation marks indicate that the local header files of the project are imported, if the header file name you see is enclosed in angle brackets, the header file is read-only for your project because it belongs to the system. If the header file name is enclosed in quotation marks, you (or others involved in this project) can edit it.

 

2. Objective-C provides a method to reduce the impact of re-compilation caused by dependency. The dependency problem exists because the Objective-C compiler requires some information to work. Sometimes, the compiler needs to know all information about the class, such as its instance variable configuration and all classes it inherits. Sometimes, the compiler only needs to know the class name, and does not need to understand its entire meaning.

For example, after an object is composite, this composite uses a pointer to the object. This works because all Objective-C objects are stored in the dynamically allocated memory. The compiler only needs to know that this is a class. Then it finds that the instance variable is the pointer size, and the pointer size will not change throughout the program.

Objective-C introduces the keyword @ class to tell the compiler: "This is a class, so I only need to reference it through pointers ." In this way, the compiler will "Calm down": it does not need to know more about this class, as long as it understands that it is referenced through pointers.

@ Class creates a forward reference. It is telling the compiler: "Believe me, you will know what this class is in the future, but now, you only need to know this." If there is a circular dependency, @ class is also useful. That is, Class A uses Class B, and Class B also uses Class. If you try to use the # import statement to make the two classes reference each other, a compilation error occurs. However, if @ class B is used in A. h and @ class A is used in B. h, the two classes can be referenced from each other.

 

3. Sometimes, we import Engine. h In the file instead of <Cocoa/Cocoa. h>. Why? We know that <cocoa/Cocoa. h> has been imported into Engine. h, so we do not need to import it again. However, if you want to add # import <Cocoa/Cocoa. h>, this is acceptable, because # The import command is intelligent enough, and it does not repeatedly import its own files.

 

4. During the learning process, we understand the cross-file dependency. In this relationship, the header file or source file must use the information in another header file. Repeated file imports increase the number of compilation times and lead to unnecessary repeated compilation. The clever use of the @ class command can reduce the Compilation Time. @ class tells the compiler that "Believe me, you can finally understand the class of this name", which can reduce the number of header files that have to be imported.

 

References: Objective-C basic tutorial

 

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.