@class and #import区别 in iOS

Source: Internet
Author: User

Many students who are just beginning to learn iOS development may find that some #import operations are written in M files when they look at other people's code, and H file only uses @class to declare, can't help wondering why not directly put #import in h file?
This is because when the H file is modified, all files that import the H file must be re-built, so if the #import is written in H, the files that import the H file will generate unnecessary compilation, increasing the compilation time, especially in the case of many project files. Imagine, if just modify an H file and result in hundreds of files unnecessary compilation, it is a very tangled thing ...
For @class just tell the compiler to have this class, please do not give an error or warning, so it will not affect the compilation.
When to use @class this way to declare better than #import?
StackOverflow on the experts gave a lot of advice:
Randy Marsh:
When I develop, I had only three things on 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 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 is some common examples of physical dependence:
any C or C + + value (a pointer or reference is not a physical dependency). If you had acgpoint as an Ivar or property, the compiler would need to see the declaration ofcgpoint.
your superclass.
a method you use. (The landlord English is not good, hope Daniel self-translation)
Here's a look at the difference between #import and class
In iOS we often introduce classes in. h and. M and so on, and so on, and so on, you might also see the #import in the. h file @class to declare, so what's the difference between #import and @class declaration? Let me say
1.import will contain all the information about this class, including the entity variables and methods, and @class only tells the compiler, the name of the declared class, as to how these classes are defined, and for the time being, we'll tell you later, so if you declare a class with @class in the header file, In the implementation of. m, if you use a specific method or variable that declares a class, you have to #import the class
2. When declaring in the. h header file with #import, if the 100 header files are #import the same header, or if the files are referenced in sequence, such as A->b,b->c,c->d, when the first header file has changed and is compiled, All of the subsequent classes that reference it need to be recompiled, and if the class that references the first header file is a lot, then it will take a lot of time, and @class will not, and someone might think about it. h just use @class just a simple declaration that the compiler has this class to not let it error, So, when you want to use the methods and properties of the introduced class in M, it is not necessary to #import the header file once, yes this is true, but the compiler compiles only the header file, so your. M with #import is not much related to compile time
3. Next say when to use @class, when should be declared with #import,
(1) Generally if there is an inheritance relationship with #import, such as B is a subclass then declare a in B with #import
(2) In addition, if there is a cyclic dependency, such as: a->b,b->a such interdependence, if in two files in the header file with #import separate declaration of each other, then there will be a header file recycling error, then in the header file with @class declaration will not be error
(3) There is the time to customize the proxy, if you want to declare the agent in the header file, such as @interface

Original: http://www.cnblogs.com/xiaohaoweiye/p/3892249.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.