The difference between #import and @class in Objective-c

Source: Internet
Author: User

Objective-c, you can use #import and @class to refer to other types, but do you know what the difference is?

@class is called Forward-class, you often see references through @class in the definition of a header file, because when you introduce a class with only @class, the compiler knows that there is a class, that is, it can recognize the engine *engine;

In the implementation file, if you want to access Engine.price, the compiler will make an error, even if you use the @class introduced. This is actually #import to introduce the header file of Engine.h, which includes all the definitions of this class, It knows that you have defined a price, and that there is no specific information introduced by @class except the class itself.

For more detailed analysis:

In Objective-c, when a class needs to refer to an interface of another class, a pointer to the referenced class needs to be established in the header file of the class

Person.h

@interface Person:nsobject

{

Woman *woman;

Man *man;

}

.......

If you compile directly, you will get an error because you do not know what Woman,man is.

At this time there are two options, one is the import of the two referenced class header file, the other is to use @class declaration woman and man is the class name, the difference between the two:

1. Import will contain all the information for this class, including entity variables and methods, and @class just tells 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, generally only need to know the name of the referenced class can be, do not need to know the internal entity variables and methods, so in the header file is generally used @class to declare that the name is the name of the class, and in the implementation of the class part , Because the internal entity variables and methods of this reference class are used, you need to use #import to include the header file of the referenced class.

3. In terms of compilation efficiency, if you have 100 header files that are #import the same head file, or if the files are referenced 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. Instead of using @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 Using @class to declare each other in the header files of two classes, there will be no compilation errors.

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 .

Reprint Address: http://blog.csdn.net/guitk/article/details/7712679

The difference between #import and @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.