A brief analysis of the use of classification Category in Objective C

  • 2020-05-19 05:59:26
  • OfStack

No matter how well a class is designed, it is inevitable that it will encounter unexpected requirements. How to extend the existing class? Of course, inheritance is a good option. But Objective-C provides a special way to extend classes, called Catagory, by dynamically adding new behavior to existing classes. This ensures that small changes to the original class add the required functionality. When extending a class with Category, we don't need to access its source code or subclass it, so we can extend the classes provided by the system. In a simple way, Category implements the modularization of class-related methods, assigning different class methods to different classification files.
Using Object-C's taxonomy, Category, is a compile-time tool that allows us to extend a class by adding methods to it (though category cannot add new instance variables), and we can do this without having to access the code in the class, similar to javascript's use of stereotypes to define properties.
We can create a new method for a class without having to edit the class definition in the code.
The following is an example program to define and use the classification. With the following code, we can add the camelCaseString classification to NSString in Object-C. Using the camelCaseString method, we can remove the space in a string and change the word after the space to uppercase (to convert the string to hump).
See how Category works with a simple example.


#import <Foundation/Foundation.h> 
 
/*
The process of defining classification can be roughly divided into the following steps:
    The first 1 Step, create, 1 A new file with an interface that creates an existing class
 
    The first 2 Step. In the new file, add the method to be extended and the implementation of the method, that is, the category to be added
 */ 
//NSString Represents the name of the class to be added, which must already exist.  
//CamelCase Is the name of the method added to the class.  
// You can only add methods, not variables.  
// Header file naming convention :ClassName+CategoryName.h 
@interface NSString (CamelCase) 
 
-(NSString*) camelCaseString; 
 
@end 
 
@implementation NSString (CamelCase) 
 
-(NSString*) camelCaseString 

    // call NSString The internal method to get the hump string.  
    //self Points to the class to which the classification has been added.  
    NSString *castr = [self capitalizedString]; 
     
    // Create an array to filter out Spaces , Combining characters with delimiters.  
    NSArray *array = [castr componentsSeparatedByCharactersInSet: 
                      [NSCharacterSet whitespaceCharacterSet]]; 
     
    // Output the array's characters  
    NSString *output = @""; 
    for(NSString *word in array) 
    { 
        output = [output stringByAppendingString:word]; 
    } 
     
    return output; 
     

 
@end 
int main (int argc, const char * argv[]) 

     
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 
     
    NSString *str = @"My name is bill."; 
    NSLog(@"%@", str); 
    str = [str camelCaseString]; 
    NSLog(@"%@", str); 
     
    [pool drain]; 
    return 0; 


What are the usage scenarios for Category?
1. The class contains a number of method implementations that require different team members to implement
2. When you are using classes in the base class library and you don't want to inherit from those classes and just want to add a few methods.

Category can realize the above requirements, of course, there are also problems to be paid attention to when using Category:
1. Category can access the instance variables of the original class, but cannot add instance variables. If you want to add variables, you can do it by creating subclasses by inheritance.
2. Category can overload the methods of the original class, which is not recommended. It will overwrite the methods of the original class. If you do want to override, you do so by subclassing by inheritance.
3. Unlike normal interfaces, you don't have to implement all the declared methods as long as you don't call the instance methods in the Category implementation file.


Related articles: