IOS Learning -- difference between @ class and # import,
During iOS development, we often see # import as the import package in some source code, but sometimes we also see @ class as the import package, so what are the differences between the two methods?
I. Main differences between @ class and # import
First, let's talk about the main differences:
- Use # import to introduce a Class header file. during compilation, all information in the Class header file will be introduced, including attributes and methods.;
- Using @ class only tells the compiler that this is a classDoes not include other information about the class, but we only care about this. We do not need to know the Internal Attributes and methods of the class, therefore, using @ class can improve compilation performance.
2. Application scenarios of @ class
@ Class has two main application scenarios:
- GenerallyUse @ class in the. h header file to Improve compilation PerformanceIn. in m implementation files, you often need to know the internal information of the class. In this case, you need to use # import to introduce this information. In many iOS source files. many of the hfiles use the @ class Method to export packages, for example, in the UIView. the H file contains the following code:
@class UIBezierPath, UIEvent, UIWindow, UIViewController, UIColor, UIGestureRecognizer, UIMotionEffect, CALayer, UILayoutGuide;
- ToAvoid loop reference between two classesIn the. h file of a class, we use @ class for import.
Next we will focus on the second scenario. For cyclic dependency, for example, Class A references class B and class B also references Class A. For example, in the following case, the code that we use # import for this nested reference will report an error during compilation.
//. H file # import "B. h "@ interface A: NSObject @ property (strong, nonatomic) B * _ B; @ end // B. h file # import ". h "@ interface B: NSObject @ property (strong, nonatomic) A * _ a; @ end
To solve this problem, we can use the @ class Method for reference,Use # import, use @ class, or use @ class to avoid errors..
//. H file @ class B @ interface A: NSObject @ property (strong, nonatomic) B * _ B; @ end // B. h file @ class A @ interface B: NSObject @ property (strong, nonatomic) A * _ a; @ end