Understanding the meaning of @ class in objective-C

Source: Internet
Author: User

Original post address: http://blog.prosight.me/index.php/2009/09/347

In objective-C, when a class needs to reference another class, that is, to establish a composite relationship, you need to create a pointer to the referenced class in the Class header file. For example:

Car. h

#import
  @interface Car:NSObject
{
    Tire *tires[4];
    Engine *engine;
}
 ...

The implementation class is omitted first. If you compile it like this, the compiler will report an error, telling you that it does not know what tire and engine are.

At this time, there are two options: one is the import header file of the two referenced classes, and the other is to declare tire and engine as class names using @ class. The difference between the two is:

  1. Import will contain all information about this class, including entity variables and methods, while @ class only tells the compiler that the name declared after it is the name of the class. As for how these classes are defined, don't worry about it for the moment. I will tell you later.
  2. In header files, you only need to know the name of the referenced class. You do not need to know its internal entity variables and methods. Therefore, in header files, @ class is generally used to declare this name as the class name. In the implementation class, because the internal object variables and methods of the referenced class are used, # import must be used to include the header file of the referenced class.
  3. In terms of compilation efficiency, if you have 100 header files # imported 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, all the classes that reference it later need to be re-compiled. If there are many classes, this will take a lot of time. Instead, @ class is not used.
  4. If there is a circular dependency such as a-> B, B-> A, if # import is used to contain each other, a compilation error occurs, if @ class is used to declare each other in the header files of the two classes, no compilation error occurs.

Therefore, in general, @ class is placed in the interface, just to reference this class in the interface and use this class as a type. In the implementation class that implements this interface, if you need to reference the object variables or methods of this class, you still need to import the classes declared in @ class.

For example:
A.h

@class Rectangle;
@interface A : NSObject {
...
}

A.M

#import Rectangle
@implementation A
...

 

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.