Analysis of the differences between import include and @class in iOS development

  • 2020-06-23 02:01:45
  • OfStack

1. 1 Generally, #import is used when importing objective c header files and #include when containing c/c++ header files.

2. #import make sure that a file can only be imported once, so you won't have problems with recursion inclusion. < tag >

So the advantage #import has over #include is that it doesn't cause cross-compilation.

#import & & #class:

1. import will contain all the information about the class, including entity variables and methods (in the.h file), while @class just tells the compiler that the names it declares later are the names of the classes, and how these classes are defined will tell you later.

2. In the header file, generally you only need to know the name of the referenced class. You don't need to know its internal entity variables or methods, so use @class in the header file to declare that the name is the name of the class. In the implementation class, #import is used to contain the header file of the referenced class because it USES the entity variables and methods inside the referenced class.

Note: #import simply takes the header file of the referenced class and includes the variables and methods in the.h file once and only once, while @class does not, so the latter compilation is more efficient.

3. In terms of compilation efficiency, if you have 100 header files with #import in the same header file, or if the files are referenced in sequence, such as A wok > B, B � > C, C � > Reference relationships such as D. When the initial header file changes, all subsequent classes that reference it need to be recompiled, which can be time-consuming if you have a large number of classes. I'm using @class but not.

4. If there is a cyclic dependency, e.g. A > B, B � > A dependency like A would cause a compilation error if you used #import to include each other, and no compilation error if you used @class to declare each other in the header file of the two classes.

Note: Practice has proved that A,B mutual #import will not cause compilation errors. because < tag > It has been stated in #import that the file was imported only once, so this is not true.

Conclusion:

1. If it's not c/c++, try using #import.

Can be #import in the implementation file, not #import in the header file.

3. Can be #import in @class + implementation file, not #import in header file.


Related articles: