I. When using C ++ for development, we can easily add new methods to existing classes using inheritance combinations to expand existing classes. However, in object-C, it is more convenient to add new methods to existing classes. Therefore, we need to use a special attribute category, however, when we use category again, we should pay attention to the following two points:
1: If you add a new method name to an existing class that is the same as the existing method name, the existing class (equivalent to Rewriting Method) will be overwritten, So that you cannot access the previous method;
For example, we want to add a sort method to nsarray and 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 {
// Specific implementation method
}
@ End
2: You can only add methods to existing classes, but cannot add attributes (variables );
However, we can use inheritance to add new property variables to existing classes.
@ Interface nsaddarray: nsarray {
Nsobject * OBJ;
}
-(ID) dosomething :( nsobject *) OBJ;