OC learn those things: @ Class keyword

Source: Internet
Author: User

1. Two methods to reference a class

@ Class: If you only define member variables and attributes

# Import: If a class is inherited

2. Differences between the two methods

# The import method contains all information about the referenced class, including the variables and methods of the referenced class;

# Import <Foundation/Foundation. h> // # import "book. H "similar to @ interface book: nsobject @ end @ interface Student: nsobject {book * _ book;} @ Property Book * book; @ end

@ Class only tells the compiler that in A. H file, B * B is only a declaration of the class. You do not need to know the information about the class. When the implementation file really needs it, it will actually view the information in Class B.

3. Correct Use

Student. h file: 

// Import <Foundation/Foundation. h> is used to know the members and methods of the foundation framework and avoid repeated definitions. # Import <Foundation/Foundation. h> // The class @ class book; @ interface Student: nsobject {book * _ book;} @ Property Book * book; @ end

Student. M file:

// When using it, tell the Member and Method

#import "Book.h" @implement Student {     … … } @end

4. @ Class Method

The @ class method is used because only the name of the referenced class (Class B) needs to be known, and the entity variables and methods in the referenced class need to be used in the Implementation class. # import must be used in the M file to include the header file of the reference class.

If hundreds of headers are all # imported to the same file, or these files are # imported in sequence, once the header file is changed, all the classes referenced in this file need to be re-compiled, so the efficiency can be imagined. In contrast, this problem will not occur in the @ class method.

# Import reports an error when a circular dependency relationship occurs.

A. H file

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

B. H file

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

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.