First,
#import
and the
#include
the Difference
When we use two # # # # In the code, we get an error: Because # include is equivalent to the declaration in the copy header file, the duplicate definition error is reported
But the use of two times #import words, will not error, so he can solve the problem of repeated import, he will make a judgment, if you have been imported once did not import
second, the key word
@class
the role
Take a look at the role of the keyword @class in OC, and before looking at his role, let's look at a question:
Now there's a course class classes and student class student that they need between two
Mutual References
(
Import
)。 Directly look at the code comparison directly:
Classes.h
[OBJC] View Plaincopy
1.//
2.//Classes.h
3.//[email protected]
4.//
5.//Created by Jiangwei on 14-10-11.
6.//Copyright (c) Jiangwei 2014. All rights reserved.
7.//
8.
9. #import <Foundation/Foundation.h>
10.
#import "Student.h"
12.
13.//Will not copy the Student.h, just tell the compiler student this class is defined elsewhere, so you do not know any information in this class (which properties and methods)
//@class Student;
15.
16.
@interfaceclasses:nsobject{
17.
18.
@public
19.
Student*_student;
20.}
21st.
22.-(
void) T1;
23.
24.
@end
Import Student.h Header File
CLASSES.M
[OBJC]View Plaincopy
1.//
2.//CLASSES.M
3.//[email protected]
4.//
5.//Created by Jiangwei on 14-10-11.
6.//Copyright (c) Jiangwei 2014. All rights reserved.
7.//
8.
9. #import "Classes.h"
10.
One.//#import "Student.h"
12.
13.
@implementationClasses
14.
15.-(
void) t1{
[_student work];
17.}
18.
19.
@end
Take a look at the student class
Student.h
[OBJC] View Plaincopy
1.//Student.h
2.//[email protected]
3.//
4.//Created by Jiangwei on 14-10-11.
5.//Copyright (c) Jiangwei 2014. All rights reserved.
6.//
7.
8. #import <Foundation/Foundation.h>
9.
#import "Classes.h"
11.
12.
@interfacestudent:nsobject{
13.
Classes*_classes;
14.}
15.
16.-(
void) work;
17.
18.
@end
The same also imports the Classes.h header file
STUDENT.M
[OBJC]View Plaincopy
1.//
2.//STUDENT.M
3.//[email protected]
4.//
5.//Created by Jiangwei on 14-10-11.
6.//Copyright (c) Jiangwei 2014. All rights reserved.
7.//
8.
9. #import "Student.h"
10.
11.
@implementationStudent
12.
13.-(
void) work{
NSLog (@ "work");
15.}
16.
17.
@end
Take a look at the test code:
main.m
[OBJC]View Plaincopy
1.//
2.//MAIN.M
3.//[email protected]
4.//
5.//Created by Jiangwei on 14-10-11.
6.//Copyright (c) Jiangwei 2014. All rights reserved.
7.//
8.
9. #import <Foundation/Foundation.h>
10.
#import "Classes.h"
#import "Student.h"
13.
//classes and student are having problems importing each other.
15.//We can use @class to solve this problem
16.//We generally use @class in. h files because the properties and methods of the class are generally not used in the. h file.
17.//Can import. h files in. m files
18.
intMain
intargc
Const
Charchar* argv[]) {
19.
@autoreleasepool{
20.
Classes*cls =[[classes alloc] init];
21st.
Student*stu = [[Student alloc] init];
Cls->_student = Stu;
[CLS T1];
24.
25.}
26.
return0;
27.}
Here's a question to note:
We have seen how this is used
Classes
the properties in
_student
, as in
Classes
properties in class
_student
is a
@public
, so it can be used directly, with the arrow symbols, and
C + +
the pointer variable in the call is similar. Remember, here's how to access directly from a class
@public
property variables for modifiers
We compile and run:
See, compile error, say in the student class of the classes class cannot find the specified type, it is strange, we did not import the Classes.h file, why still can't find the type?
This is the problem of the mutual reference of the class in OC, we import the Classes.h in the Student.h file, and in the Classes.h file import the STUDENT.H,OC in the mutual import when the compilation is not passed
This can lead to errors.
The solution, of course, is to use
@classkeyword, we modify the classes class
Classes.h
[OBJC]View Plaincopy
1.//
2.//Classes.h
3.//[email protected]
4.//
5.//Created by Jiangwei on 14-10-11.
6.//Copyright (c) Jiangwei 2014. All rights reserved.
7.//
8.
9. #import <Foundation/Foundation.h>
10.
One.//#import "Student.h"
12.
13.//Will not copy the Student.h, just tell the compiler student this class is defined elsewhere, so you do not know any information in this class (which properties and methods)
14.
@classStudent;
15.
16.
@interfaceclasses:nsobject{
17.
18.
@public
19.
Student*_student;
20.}
21st.
22.-(
void) T1;
23.
24.
@end
We will #import the "Student.h" code, using the
@class Student
The purpose of this code is not to copy the Student.h file, but to tell the compiler student that the class is defined elsewhere, so that no information is known in this class (which properties and methods)
CLASSES.M
[OBJC]View Plaincopy
1.//
2.//CLASSES.M
3.//[email protected]
4.//
5.//Created by Jiangwei on 14-10-11.
6.//Copyright (c) Jiangwei 2014. All rights reserved.
7.//
8.
9. #import "Classes.h"
10.
#import "Student.h"
12.
13.
@implementationClasses
14.
15.-(
void) t1{
[_student work];
17.}
18.19.
@endThen in the classes.m file import #import "Student.h" file, this time the compilation will not error, the same can be normal operation.
#import和 the role of the Difference keyword @class # include