Differences between #import and @class in IOS

Source: Internet
Author: User

1. #import will contain all the information about this class, including the entity variables and methods, and @class just tell the compiler that the name that is declared after it is the name of the class, as to how these classes are defined, for the time being, will tell you later.

2. In the header file (. h file), it is generally only necessary to know the name of the referenced class. There is no need to know the entity variables and methods inside, so it is common to use @class in the header file to declare that the name is the name of the class. In the implementation class (. m file), because the internal entity variables and methods of the referenced class are used, it is necessary to use #import to include the header file of the referenced class.

3. In terms of compilation efficiency, if you have 100 headers #import the same header file, or these files are quoted in turn, such as A–>b, B–>c, C–>d, and so on. When the first header file changes, all the subsequent classes that reference it need to be recompiled, which can take a lot of time if you have a lot of classes. With @class, you don't.

4. If there are cyclic dependencies, such as A–>b, b–>a such interdependencies, if the use of #import to each other, then there will be a compilation error, if you use @class in the header files of two classes are declared in each other, there will be no compile errors appear.

So, in general , the @class is placed in the. h file, just to refer to the class in the. h file and use the class as a type. In the. m file that implements this interface, if you need to reference the entity variables or methods of this class, you need to #import在 the classes declared in the @class.

Give an example to illustrate.

In the ClassA.h
#import ClassB.h equivalent to include the entire. h header file. If there are many. m files #importclassa.h, these files will also #importClassB when compiling. h added unnecessary #import and wasted compilation time. In large software, reduce the #include是非常重要的 in the. h file.
If, just ClassB, there is no #includeClassB. H. Only need to #import ClassB.h in. m files that need to be ClassB.

So when can I use it?
If you only need to declare a pointer to a ClassB in ClassA.h, you can declare the @ClassB in ClassA.h;

Suppose that there are two classes: ClassA and CLASSB, two of which are used to each other, which constitutes the circulardependency (cyclic dependency). If in the header file only uses #import to contain each other's header file (constitutes circularinclusions, the loop contains ), the compiler will error:

Expected specifier-qualifier-list before ' ClassA '

Or

Expected specifier-qualifier-list before ' ClassB '

In order to avoid the loop contains, in the ClassA.h file with @class ClassB; Include the ClassB in the same way that the ClassA is included in the ClassB.h file with @classClassA. The @class instruction just tells the compiler that this is a class, leaving a space to hold the pointer. Next, it is likely that in CLASSA.M and CLASSB.M there will be access to the members of the included objects, you must let the compiler know more information, such as what the class can be called, you must use the #import, again the use of the class contained in, Tells the compiler what additional information is required. Otherwise, the compiler warns you:

Warning:receiver ' ClassA ' is a forward class andcorresponding @interface could not exist

In another case, use the Categories class to include the Categories in the. h header file with #import.

In short, the principle of use is:

In the header file only #import the superclass message file inside #import need to send a message in the past, the class used @class to turn to the declaration.

Differences between #import and @class in IOS

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.