In objective-C, when a class is used to another class and the referenced pointer needs to be created in the Class header file,
I. # import and @ Class
The following code is used:
A. H file
#import "B.h"@interface A : NSObject { B *b;}@end
For the sake of simplicity: Class A is a reference class, and Class B is a referenced class. The implementation file of Class A is not considered here.
There are two methods to reference a class:
One is introduced through # import; the other is introduced through @ class;
The difference between the two methods is:
1. # The import method contains all information about the referenced class, including the variables and methods of the referenced class. The @ class method only tells the compiler in A. H file.
B * B
It's just a declaration of the class. You don't need to know the information in the specific class. You can view the information in Class B only when the implementation file is actually used;
2. The @ class method is used because only the name of the referenced class (Class B) is required, and the entity variables and methods of the referenced class are used in the Implementation class, therefore, you need to use # importl to include the header file of the referenced class;
3. Through the above two points, it is easy to know the compilation efficiency. If there are hundreds of header files # import the same file, or these files are sequential by # improt (a-> B, B-> C, C-> D ...), Once the header file is slightly changed, all the classes referenced in this file need to be re-compiled. This efficiency can be imagined, but relatively speaking, this problem will not occur when @ class is used;
4. For cyclic dependency, for example, Class A references Class B, and Class B also references Class A and Class B code:
#import "A.h"@interface B : NSObject { A *a;}@end
When the program runs, an error is reported during compilation,
When @ class is used to declare each other in two classes, no compilation error occurs.
It can be seen from the above that @ class is placed in the interface, just referencing a class, using this referenced class as a type, in the implementation file, if you need to reference the object variable or method of the referenced class, you also need to use the # import method to introduce the referenced class.
For example:
A. m file
#import "A.h"#import "B.h"@implementation A......
Ii. Differences between # import <...> and # import "..."
# Import <……> As a result, the Preprocessing Program only searches for contained files in the special "system" header file directory. The current directory is not searched.
# Import "...... "The Preprocessing Program is in one or more file directories.
Reproduced from: http://jordy.easymorse.com /? P = 757