Category (category) in objective-c, extension (Extension)

Source: Internet
Author: User

The purpose of the class and extension is to extend a class.

Category (category) in the Objective-c

I. Definition and role of class purpose

Category is also called classification, the English category, in the absence of the original class. m file based on, to add methods to the class.

For example, NSString class originally did not have hellostring method, we do not have nsstring class of original code, then we want to add a hellostring method to the NSString class, only by declaring subclasses or classes can achieve this purpose.

II. Declaration of the purpose of the class:

@interfaceNSString (Hello)

-(void) hellostring;

@end

III. realization of the Category:

@implementationNSString (Hello)

-(void) hellostring

{

NSLog (@ "My name is%@,i am-a string", self);

}

@end

Filename:

Nsstring+hello.h

Use:

NSString * str = @ "I want Heaven";

[Strhellostring];

Print output:

My name is I want to heaven, I am a string

The self used in the hellostring implementation is the instance itself that invokes the method.

Iv. issues needing attention in the use of categories:

1, the class can not add the instance variable, but the class declaration may use the attribute, in fact, the property is a pair of methods, then in. m need to implement this property setter method and Getter method, in these two implementation methods still cannot use the instance variable that you add.

2. The method added in the category cannot have the same name as the method in the original class, otherwise it will result in overwriting.

3. A class can add multiple classes, but the class name and method name cannot be duplicated.

4, the method in the class can become part of the original class, and the original class method level, you can inherit the quilt class.

Limitations: You can only add methods to classes in the class, you cannot add instance variables, and the methods in the class have a higher priority.

Extension in the Objective-c (Extension)

Extension is also called extension:

Extension is a special form of the category, mainly in a class of. m inside Declaration and implementation

The function of extension: To add a private method or a private variable to a class

Like ROOTVIEWCONTROLLER.M.

@interface Rootviewcontroller ()

{

NSString * astring;

}

-(void) test1;

-(void) test2;

@end

@implementation Rootviewcontroller

-(ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) Nibbundleornil

{

self = [super InitWithNibName:nibNameOrNilbundle:nibBundleOrNil];

if (self) {

Custom initialization

}

return self;

}

@end

The above @interface @end defines an extension that mainly defines Rootviewcontroller private methods and private variables.

Five, the use of extension need to pay attention to several issues:

1, if there is no class name in the brackets, it is considered that the method in the extension must be implemented, if famous, it is considered an optional implementation.

2, although the extension is to a class to define a private method, but no OC has no absolute private method, in fact, can be called, in addition to the extension of the declaration of the variables can only be used inside the class, the outside access.

3. You cannot add an instance variable if a class extension. h file is built for a new file.

Category (category) in objective-c, extension (Extension)

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.