Objective-C's understanding about import and @ Class

Source: Internet
Author: User

In objective-C, when a class is used to another class and the referenced pointer needs to be created in the Class header file,

I. # import and @ Class

The following code is used:

A. H file

#import "B.h"@interface A : NSObject {    B *b;}@end

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

There are two methods to reference a class:

One is introduced through # import; the other is introduced through @ class;

The difference between the two methods is:

1. # The import method contains all information about the referenced class, including the variables and methods of the referenced class. The @ class method only tells the compiler in A. H file.

B * B

It's just a declaration of the class. You don't need to know the information in the specific class. You can view the information in Class B only when the implementation file is actually used;

2. The @ class method is used because only the name of the referenced class (Class B) is required, and the entity variables and methods of the referenced class are used in the Implementation class, therefore, you need to use # importl to include the header file of the referenced class;

3. Through the above two points, it is easy to know the compilation efficiency. If there are hundreds of header files # import the same file, or these files are sequential by # improt (a-> B, B-> C, C-> D ...), Once the header file is slightly changed, all the classes referenced in this file need to be re-compiled. This efficiency can be imagined, but relatively speaking, this problem will not occur when @ class is used;

4. For cyclic dependency, for example, Class A references Class B, and Class B also references Class A and Class B code:

#import "A.h"@interface B : NSObject {    A *a;}@end

When the program runs, an error is reported during compilation,


When @ class is used to declare each other in two classes, no compilation error occurs.

It can be seen from the above that @ class is placed in the interface, just referencing a class, using this referenced class as a type, in the implementation file, if you need to reference the object variable or method of the referenced class, you also need to use the # import method to introduce the referenced class.

For example:

A. m file

#import "A.h"#import "B.h"@implementation A......
Ii. Differences between # import <...> and # import "..."

# Import <……> As a result, the Preprocessing Program only searches for contained files in the special "system" header file directory. The current directory is not searched.

# Import "...... "The Preprocessing Program is in one or more file directories.

Reproduced from: http://jordy.easymorse.com /? P = 757

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.