Objectios basics: understand the meaning of @ class in objective-C

Source: Internet
Author: User
Understanding the meaning of @ class in objective-C

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

 

The following code is used:

 

A. H file

C code

  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, 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 the information about the referenced class, including the variables and methods of the referenced class. The @ class method only tells the compiler that. in the H file, B * B is only a class declaration. You do not need to know the specific information in this class. When the implementation file is actually used, to view the information in Class B;

 

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
Files, or these files are # 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, and
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:

  1. # Import "A. H"
  2. @ Interface B: nsobject {
  3. A *;
  4. }
  5. @ 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

  1. # Import "A. H"
  2. # Import "B. H"
  3. @ Implementation
  4. ......

 

Here is an example.

 

In classa. h

# Import classb. H is equivalent to # include the entire. h header file. If there are many. m Files # import classa. H, these files will also # import classb. h added unnecessary # import during compilation, wasting Compilation Time. In large software, it is very important to reduce the include in. H files.

 

If it is just classb, there is no include classb. h. You only need to # import classb. h In the. M file that requires 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;

 

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.

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.