Category Introduction
In everyday development, you may encounter the need to add methods to a class. For example, you need to add a printing method to the NSString class. Of course, we can create a new class such as TestString and inherit the NSString class to implement the DisplayString method in the new class teststring. However, there is one obvious flaw in this approach: only the TestString class has the method, and other subclasses of the NSString class, such as nsmutablestring, cannot use the method. Is it possible to add a method to the NSString class so that all subclasses of NSString and nsstring can be used? The answer is yes, the category can solve the problem perfectly.
Use of Category
The syntax for using category in Objective-c is to use the @interface keyword, which is very similar to defining a standard class, but not using a colon (:, which is used when inheriting a class), but instead using (), as follows:
@interface nsstring (playstring)-(void) playstring: (NSString *) content; @end
Where: The playstirng in parentheses is the category name.
You can add a category to any class, even if you don't see the source code for the class. When adding cateogry to a class, this class and all subclasses of the class can use the methods in the category. At run time, there is no difference between the methods in the category and the original code in the class. For example, in the example above, the NSString class added cateogry,category defines the Playstring method, which is implemented as follows:
-(void) playstring: (NSString *) content{ NSLog (@ "Thecontent is%@" , content);}
This allows you to use the Playstring method for instance objects of the NSString class, as well as instance objects for all subclasses of the NSString class. As follows:
@" This is original NSString " ; [MyString playstring:mystring]; *mutstring = [[nsmutablestring alloc] init];[ Mutstring appendString:@ "This isa subclass of NSString"]; [Mutstring playstring:mutstring];
In addition to adding methods to a class, the category has the following two usage scenarios:
1: Split a large, complex class file into several small class files.
2: When multiple people develop the same class file, they can use category to develop their own functions.
Considerations for using Category
The name of the method in 1:category.
(1): Try not to have the same name as the method in the original class, although this is legal, but the same name as the method in the original class is definitely not a good programming habit. As a result of this, neither the original class nor the subclass of the original class can use that method in the original class. In general, if you want to overwrite a method in a parent class, it is more appropriate to implement it with inheritance than category.
(2): When a primitive class has more than one category, the method names in each category remain distinct. Although the method name repetition in multiple category does not alert the error, some inexplicable errors occur. When the method names in multiple categories are duplicated, each category adds a function to the original class, so that at run time, the method that is called may be inconsistent with what we expect. In this case, the specific invocation of the method in the category is related to the compiler.
Instance variables cannot be added in 2:category. Although you can add attributes in the category, in. m files, the compiler does not automatically synthesize instance variables and access the Getter/setter method for instance variables. To add an instance variable to an original class, this can be achieved with inheritance.
On the principle of Category
In fact, after a class in Objective-c is compiled, there is a list of methods in memory, and the method list points to the code block address of the method. When you send a message to a method, you look for the method from the list of methods. For example, there is a class person that compiles a list of methods that are generated: SetName, GetName, getsex .... Now that the class adds a category,category also implements the method GetName, then once again compiled, the resulting method list is: SetName, getName (category), GetName (original Class), Getsex ... When sending a message to the GetName method, looking from the list of methods in the class and finding the first GetName method, it is not going to continue to look down, so it is always used as the GetName method implemented in the category. This is why you should pay attention to the name of the method in category.
Objective-c: Category