Difference between @ class and # import in ios,
Many new iOS developers may find that some of the # import operations are written in the m file when reading other people's code, while the H file only uses @ class for declaration, why don't I put # import directly into the H file?
This is because after the H file is modified, all the files that import the H file must be re-built. Therefore, if you write # import in the H file, the file for importing the H file will generate unnecessary compilation and increase the compilation time, especially when there are many project files. Imagine how hard it would be to modify an H file and cause unnecessary compilation of hundreds of files...
For @ class, it only tells the compiler that this class exists. Do not report errors or warnings, so it will not affect compilation.
When is it better to use @ class to declare it than # import?
Stackoverflow experts gave a lot of suggestions:
Randy Marsh:
When I develop, I have only three things in mind that never cause me any problems.
1. Import super classes
2. Import parent classes (when you have children and parents)
3. Import classes outside your project (like in frameworks and libraries)
For all other classes (subclasses and child classes in my project self), I declare them via forward-class.
Justin:
Simple answer: You # import or # include when there is a physical dependency. Otherwise, you use forward declarations (@ class MONClass, struct MONStruct, @ protocol MONProtocol ).
Here are some common examples of physical dependence:
• Any C or C ++ value (a pointer or reference is not a physical dependency). If you have aCGPoint as an ivar or property, the compiler will need to see the declaration ofCGPoint.
• Your superclass.
• A method you use)
Next, let's take a look at the # differences between import and class.
In ios, we often. h and. some classes are introduced in m. Generally, # import is used for declaration. You may also see this in. the H file is declared using @ class, so # What is the difference between the declaration of import and @ class? Let me talk about
1. import will contain all information about this class, including entity variables and methods, while @ class only tells the interpreter about the declared class name and how these classes are defined, you don't have to worry about it for the moment. I will tell you later, so if you declare a class with @ class in the header file. if the specific method or variable of the declared class is used in the implementation of m, you have to # import class
2. in. if # import is used in the h header file for declaration, if all the 100 header files # import the same header file, or these files are referenced in sequence, such as A-> B, b-> C, C-> D. When the header file at the beginning changes and the file is compiled, all the classes that reference it need to be re-compiled, if there are many classes that reference the first header file, it will take a lot of time, but @ class is not used, and some may think about it as soon as possible. h is just using @ class. It's just a simple declaration that the compiler has this class to prevent it from reporting errors. when the introduced class methods and attributes are used in m, the # import header file is not required once. Yes, this is correct, but only the header file is compiled by the compiler, so your. # import in m does not have much to do with the Compilation Time
3. Next, let's talk about when to use @ class and when to use # import for declaration,
(1) If there is an inheritance relationship, use # import. For example, if B is A subclass of A, use # import when B declares A in B.
(2) If there is A circular dependency, such as A-> B, B-> A, and so on, if the header files of the two files are declared with # import, then there will be a header file recycling error, then in the header file with @ class declaration will not make an error
(3) When the custom proxy is used, if you want to declare the proxy in the header file, such as @ interface
The property problem in Objective-C class in iOS Programming
The first method also contains @ sythesize name in implementation. This method is actually the abbreviation of @ sythesize name = name. @ sythesize XXX = YYY means, the getter and setter access functions are automatically generated for the instance variable YYY of the class. The generated function declaration is getXXX () and setXXX (xxx ). In addition, you can use a simple. Operator to access and assign values, which is equivalent to an access function.
How does iOS determine what type of object is?
[Object isKindOfClass: [NSObject class];
The returned value is of the BOOL type.
Replace NSObject with the class name you want.