IOS header file import-@classSummary of precautions
1. There are two different ways to import header files:
Use quotes or angle brackets, for example, #import <Cocoa/> and #import "". Angular bracket statements are used to import system header files, while a quoted statement means that the project is imported local header files. If the header file name you see is enclosed in angle brackets, then this header file is read-only for your project because it belongs to the system. If the header file name is enclosed in quotes, then you (or others involved in the project) can edit it.
2. Objective-C provides a way to reduce the impact of recompilation caused by dependencies.
The dependency problem exists because the Objective-C compiler requires certain information to work. Sometimes, the compiler needs to know all the information about a class, such as its instance variable configuration, all the classes it inherits, etc. Sometimes, the compiler only needs to know the class name, not the entire meaning of it.
For example, after the object is compounded, this compound uses a pointer to the object. This works because all Objective-C objects are stored in dynamically allocated memory. The compiler just needs to know that this is a class. Then it finds that the instance variable is the size of the pointer, and the size of the pointer will not change throughout the program.
Objective-C introduces the keyword @class to tell the compiler: "This is a class, so, I just need to reference it through a pointer." In this way, the compiler will "calm": it does not need to know more about this class, just know that it is referenced through a pointer.
@class creates a forward reference. It's just telling the compiler: "Believe me, you will know what this class is in the future, but now, you only need to know this". @class is also useful if there is a circular dependency. That is, Class A uses Class B, and Class B also uses Class A. If you try to make these two classes refer to each other through the #import statement, you will eventually get a compilation error. But if you use @class B and @class A in it, then these two classes can refer to each other.
3. Sometimes, we import it in the file but not the <Cocoa/>, why is this?
We know that <cocoa/> has been imported, so we don’t need to import it again by ourselves. However, if you want to add #import <Cocoa/> to that file, this is OK, because the #import command is smart enough that it won't repeatedly import its own file.
4. During the learning process, we understand cross-file dependencies, in which the header file or source file needs to use information from another header file.
Repeated imports between files will increase the number of compiles and will also lead to unnecessary duplicate compilation. While cleverly using the @class command can reduce compilation time, @class tells the compiler "Believe me, you will eventually understand the class of this name", which can reduce the number of header files that have to be imported.
Reference materials: "Objective-C Basic Tutorial"
Thank you for reading, I hope it can help you. Thank you for your support for this site!