Categories and extensions in Objective-c

Source: Internet
Author: User

Category: Adding new methods to known classes; extension: Notifications use classes to declare private methods in the definition of this class;

I. Category:

1. Application of the class method:

    • To extend an existing class: for example, you can extend the classes in the Cocoa Touch framework, and the methods added in the class are inherited by the quilt class, and are not distinguished from other methods at run time.
    • As an alternative to subclasses: you do not need to define and use a subclass, you can add methods to existing classes directly through the class.
    • Categorize methods in a class: Use Catigory to divide a large class into small chunks to be developed separately to better update and maintain the methods in the class.

2. Limitations of the class approach:

    • A new instance variable cannot be added to the class, and the class does not have a location to hold the instance variable. If you want to increase an instance variable of a class, you can only define the subclass by the way.
    • If, in the class to cover the method of the successive class. This causes the super message to break because the general method of the class has a higher priority, so it is generally not to overwrite the methods in the existing class.

3. Class Purpose Definition:

Class purpose naming rules: Class name + extension method, such as "Nsstring+revert". The class intent interface declaration is very similar to the definition of a class, but the class does not inherit the parent class, only with a parenthesis indicating the primary purpose of the class purpose.

Nsstring+revert.h file:

#import <Foundation/Foundation.h>

@interface NSString (Revert)

-(void) test;

@end

NSSTRING+REVERT.M file:

#import "Nsstring+revert.h"

@implementation NSString (Revert)

-(void) test{
}

@end

Second, extension:

The extension of a class can be thought of as an anonymous class, and the class sometimes needs to be seen only for itself, and the private method used by the private method can be declared in an extended manner, and the method defined in the extension is implemented in the @implementation code area of the class itself.

@interface Myobject:nsobject
{
NSNumber *number;

}
-(NSNumber *) number;
@end

@interface MyObject (Setter)
-(void) Setnumber: (NSNumber *) Newnumber;
@end


@implementation MyObject
-(NSNumber *) number
{
return number;
}
-(void) Setnumber: (NSNumber *) newnumber
{!//do Something
}
@end

When a class name is not provided when the extension is defined, the method defined in the extension is considered both "must implement" APIs in this case, if the method lacks the implementation code, the compiler will report a warning, at which point the implementation of the method must appear in the @implementation code block of the class body.

Categories and extensions in Objective-c

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.