iOS basics: In-depth understanding of the meaning of @class in objective-c

Source: Internet
Author: User

Objective-c, when a class is used to another class, and the referenced pointer needs to be created in the header file of the class,

As in the following code:

A.h file

    1. #import "B.h"
    2. @interface A:nsobject {
    3. B *b;
    4. }
    5. @end

For the sake of simplicity: Class A is a reference class, Class B is the referenced class, where the class A implementation file is not considered first.

There are two ways to refer to a class generally:

One is introduced by means of #import, and the other is introduced by @class;

The difference between the two approaches is that:

1, #import方式会包含被引用类的所有信息, including the variables and methods of the referenced class; @class just tell the compiler in the A.h file that B *b is just a class declaration, what information is in this class, there is no need to know, and so on when the implementation of the file is really used, To actually see the information in Class B;

2, the use of @class mode because only the name of the referenced class (Class B) can be used, and in the implementation of the class to use the referenced class in the entity variables and methods, so you need to use #importl to contain the referenced class header file;

3, through the above 2 points is also easy to know in compiling efficiency, if there are hundreds of header files are #import the same file, or these files are #improt (A->b, b->c,c->d ... ), once the initial header file changes slightly, all the classes referenced to this file will need to be recompiled again, so the efficiency is conceivable, and relatively speaking, the use of @class way will not appear this problem;

4, for circular dependency, for example Class A refers to Class B, while Class B also refers to Class A, Class B code:

    1. #import "A.h"
    2. @interface B:nsobject {
    3. A *a;
    4. }
    5. @end

When the program is running, the compilation will error,

When using @class in two classes to declare each other, there will be no compile error.

From the above, @class is placed in the interface, just reference a class, the referenced class as a type, in the implementation file, if you need to refer to the referenced class entity variables or methods, you also need to use the #import method to introduce the referenced class.

Such as:

    1. #import "A.h"
    2. #import "B.h"
    3. @implementation A
    4. ......

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 #import ClassA.h, then these files will #import ClassB.h add unnecessary #import and waste compilation time when compiling. In large software, it is important to reduce the include in. h files.

If it's just ClassB, there's no include ClassB.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 CLASSB pointer in ClassA.h, you can declare it in ClassA.h

@ClassB

...

ClassB *pointer;

So, in general, the @class is put in interface, just to refer to this class in interface to use this class as a type. In the implementation class 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 @class.

Link Address: http://www.cnblogs.com/martin1009/archive/2012/06/24/2560218.html

iOS basics: In-depth understanding of the meaning of @class in objective-c

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.