Objective-C -- Extension

Source: Internet
Author: User

Objective-C -- Extension

In my previous blog, I explained in detail the Category "Objective-C -- Category details", and the extension and Category are very similar. Today we are going to learn more and expand.

Extension is added after Objective-C 2.0. The so-called Extension actually adds additional methods or attributes to a class. These methods and attributes are private. We often turn Extension into an anonymous Category. Class extension is a very good way to declare private methods in the. m file. The declared methods in the class extension are exactly the same as those declared in the class. They must be implemented in @ implementation of the class. This method is visible only within the class and invisible to the outside world.

The difference between the Category and the Extension is as follows:

(1) create a Category from a class and generate the class name + Category name. h and class name + Category name. m files, while creating an Extension from a class generates only one class name _ Extension. h.

(2) attributes or variables cannot be defined in Category, while attributes or variables can be defined in Extension.

(3) The methods defined in Category can be inherited by the quilt class, while the methods and attributes defined in Extension cannot be inherited by the quilt class because they are private.

(4) Category can add methods to custom classes or classes in the framework. Extension can only add methods to custom classes, because you cannot implement these adding methods in the implementation file of the framework class, after all, the OC is not open source and the code is invisible.

(5) The Category name is @ interface Class Name (Category name), and @ implementation Class Name (Category name); the Extension name is only @ interface Class Name ();

(6) Extension can be understood as a special Category;

 

We use code to implement Extension:

(1) define the Person class and implement the following in Person. h:

 

#import 
 
  @interface Person : NSObject- (instancetype)initWithName:(NSString*)aName;- (void)print;@property(nonatomic,strong) NSString *name;@end
 

(2) implement the following in Person. m:

 

 

# Import Person. h // define an Extension; @ interface Person () // for common variables, you cannot use strong, retain, copy, but assign; @ property (nonatomic, assign) int age; // This is a private method. It can only be called within the class @ implementation; invisible to the outside;-(void) printName;-(void) printAge; @ end // class implementation file; @ implementation Person-(instancetype) initWithName :( NSString *) aName {// use the parent class method to initialize the object; self = [super init]; if (self) {// set the property value of this object; self. name = aName; self. age = 23;} // The returned object is returned here; return self;}-(void) print {NSLog (@ Person print); [self printName]; [self printAge];} // implement the method defined in Extension;-(void) printName {NSLog (@ name: % @, self. name);}-(void) printAge {NSLog (@ name: % d, self. age) ;}@ end

(3) implement the following in main. m:

 

 

#import 
 
  #import Person.hint main(int argc, const char * argv[]) {  @autoreleasepool {    Person *jack = [[Person alloc] initWithName:@jack];    [jack print];      }    return 0;}
 

(4) the output is as follows:

 

.

 

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.