Objective-c, when a class is used to another class, and the referenced pointer needs to be created in the header file of the class,
As in the following code:
A.h file
- #import "B.h"
- @interface A:nsobject {
- B *b;
- }
- @end
For the sake of simplicity: Class A is a reference class, Class B is the referenced class, where the class A implementation file is not considered first.
There are two ways to refer to a class generally:
One is introduced by means of #import, and the other is introduced by @class;
The difference between the two approaches is that:
1, #import方式会包含被引用类的所有信息, including the variables and methods of the referenced class; @class just tell the compiler in the A.h file that B *b is just a class declaration, what information is in this class, there is no need to know, and so on when the implementation of the file is really used, To actually see the information in Class B;
2, the use of @class mode because only the name of the referenced class (Class B) can be used, and in the implementation of the class to use the referenced class in the entity variables and methods, so you need to use #importl to contain the referenced class header file;
3, through the above 2 points is also easy to know in compiling efficiency, if there are hundreds of header files are #import the same file, or these files are #improt (A->b, b->c,c->d ... ), once the initial header file changes slightly, all the classes referenced to this file will need to be recompiled again, so the efficiency is conceivable, and relatively speaking, the use of @class way will not appear this problem;
4, for circular dependency, for example Class A refers to Class B, while Class B also refers to Class A, Class B code:
- #import "A.h"
- @interface B:nsobject {
- A *a;
- }
- @end
When the program is running, the compilation will error,
When using @class in two classes to declare each other, there will be no compile error.
From the above, @class is placed in the interface, just reference a class, the referenced class as a type, in the implementation file, if you need to refer to the referenced class entity variables or methods, you also need to use the #import method to introduce the referenced class.
Such as:
- #import "A.h"
- #import "B.h"
- @implementation A
- ......
Give an example to illustrate.
In the ClassA.h
#import ClassB.h equivalent to include the entire. h header file. If there are many. m files #import ClassA.h, then these files will #import ClassB.h add unnecessary #import and waste compilation time when compiling. In large software, it is important to reduce the include in. h files.
If it's just ClassB, there's no include ClassB.h. Only need to #import ClassB.h in. m files that need to be ClassB.
So when can I use it?
If you only need to declare a CLASSB pointer in ClassA.h, you can declare it in ClassA.h
@ClassB
...
ClassB *pointer;
So, in general, the @class is put in interface, just to refer to this class in interface to use this class as a type. In the implementation class that implements this interface, if you need to reference the entity variables or methods of this class, you need to import the classes declared in @class.
Link Address: http://www.cnblogs.com/martin1009/archive/2012/06/24/2560218.html
iOS basics: In-depth understanding of the meaning of @class in objective-c