C # If I write more, I forget that there is a Forward Declaration. It took half a day to see @ class. Write this topic today.
1. Why do we need a pre-declaration?
Pre-declaration helps avoid circular dependency. For example:
interface A:NSObject- (B*)calculateMyBNess;@end@interface B:NSObject- (A*)calculateMyANess; @end
In this way, the statement cannot be compiled, because the problem of having a chicken first or an egg first occurs.
At this time, you need to add a pre-statement:
@class B;@interface A:NSObject- (B*)calculateMyBNess;@end@interface B:NSObject- (A*)calculateMyANess; @end
@ Class tells the compiler that a class with such name exists somewhere.
2. @ class vs. # import
Syntactically, both the pre-declaration and # import can be compiled and run successfully.
So what are the application scenarios of the two?
According to the http://stackoverflow.com/questions/322597/class-vs-import,
If you see the warning:
Warning: extends er 'myolclass' is a forward class and corresponding @ interface may not exist
You need to import this file. However, you can use the @ class declaration in the header file instead of importing directly in the. h (header file), but in the. m (implementation file.
@ Class usually prevents premature import. If the compiler sees a line of syntax:
@ Class myCoolClass, it knows that it may immediately see code similar to the following:
myCoolClass *myObject;
So it will reserve a pointer space for this class, and then it will be busy with others.
However, if you need to create or access a member of myObject, only one class pointer is not enough. You need to let the compiler know what these members are. In this case, # import "myCoolClass. h" is required.
Some people simply list three rules. Due to insufficient level, translation may lead to ambiguity. I will put the original article directly:
- Only # import the super class, and adopted protocols, in header files.
- # Import all classes, and protocols, you send messages to in implementation.
- Forward declarations for everything else.
In http://stackoverflow.com/questions/6076207/objective-c-forward-declarations-vs-imports
Someone has mentioned that, based on his experience, using # import accidentally may allow the compiler to compile N more code (he used the order of magnitude of million ). As long as a header file is slightly modified, all the import classes need to be re-compiled, so it takes a long time to compile.
3. Why does C # require no pre-declaration?
I did not find any explanation after Google. It seems that almost no one is interested in C # why there is no pre-declaration...
My friend Zero and I have discussed this issue. His opinions are as follows:
"C # compiler can scan source code multiple times, so no pre-declaration is required. In theory, C ++ compiler is acceptable, but in fact C ++ compiler does not ."
Of course, C # also has circular dependencies. In this case, dependency injection (control inversion) can be used to eliminate it. For details, refer to the following:
Http://stackoverflow.com/questions/3955465/circular-class-reference-problem