**Detailed explanation of keyword category (category) developed by IOS**

One: When developing in C++, we can easily use inheritance combination to add new methods to existing classes and extend existing classes. But in object-c, it is more convenient for us to add new methods to existing classes. Then this is a unique attribute category that we need to use, but we should pay attention to two points when we use category again:

1: If adding a new method name to an existing class is the same as the existing method name, it will overwrite the existing class (equivalent to rewriting the method), so that the previous method cannot be accessed;

For example, we hope to add a sorting method to NSArray, and hope to arrange the following arrays from small to small

 NSArray *arr = [NSArrayarrayWithObjects:@"12",@"111"@"2"@"67",@"45"@"80"nil];

@interface NSArray (sortSuppot)

- (NSArray *) sortArrayByThisMod:(NSArray *)array;

@end


@implementation NSArray (sortSupport)

- (NSArray *) sortArrayByThisMod:(NSArray *)array {

//The specific implementation method

}

@end

2: You can only add methods to existing classes, not attributes (variables);

But we can use inheritance to add new attribute variables to existing classes

@interface NSAddArray : NSArray {

NSobject *obj;

}

- (id)doSomething:(NSObject *)obj;

Guess you like

Origin blog.csdn.net/u010436133/article/details/47683567