1. Two methods to reference a class
@ Class: If you only define member variables and attributes
# Import: If a class is inherited
2. Differences between the two methods
# The import method contains all information about the referenced class, including the variables and methods of the referenced class;
# Import <Foundation/Foundation. h> // # import "book. H "similar to @ interface book: nsobject @ end @ interface Student: nsobject {book * _ book;} @ Property Book * book; @ end
@ Class only tells the compiler that in A. H file, B * B is only a declaration of the class. You do not need to know the information about the class. When the implementation file really needs it, it will actually view the information in Class B.
3. Correct Use
Student. h file:
// Import <Foundation/Foundation. h> is used to know the members and methods of the foundation framework and avoid repeated definitions. # Import <Foundation/Foundation. h> // The class @ class book; @ interface Student: nsobject {book * _ book;} @ Property Book * book; @ end
Student. M file:
// When using it, tell the Member and Method
#import "Book.h" @implement Student { … … } @end
4. @ Class Method
The @ class method is used because only the name of the referenced class (Class B) needs to be known, and the entity variables and methods in the referenced class need to be used in the Implementation class. # import must be used in the M file to include the header file of the reference class.
If hundreds of headers are all # imported to the same file, or these files are # imported in sequence, once the header file is changed, all the classes referenced in this file need to be re-compiled, so the efficiency can be imagined. In contrast, this problem will not occur in the @ class method.
# Import reports an error when a circular dependency relationship occurs.
A. H file
#import "B.h" @interface A : NSObject { B *b; } @end
B. H file
#import"A.h" @interface B : NSObject { A *a; } @end