What is category
Category mode is used to add methods to existing classes to extend existing classes, and in many cases category is a better choice than creating subclasses. The newly added method is also automatically inherited by all subclasses of the extended class. When you know that a method in an existing class has a bug, but the class is in the form of a library, and when we can't directly modify the source code, category can also be used to replace the entity of a method in the existing class to fix the bug. However, there is no convenient way to invoke an existing class that has been replaced by a method entity. It is important to note that when you are ready to have a category to replace a method, be sure to implement all the functionality of the original method, otherwise this substitution is meaningless and will cause new bugs. Unlike subclasses, category cannot be used to add an instance variable to an extended class. Category is often used as a tool for organizing framework code.
Use of category
1. Implement an extension of an existing class without creating an inherited class.
2. Simplify the development of classes (when a class requires multiple programmers to develop together, category can place the same class separately in different source files based on the purpose, allowing the programmer to independently develop the appropriate collection of methods).
3. Group the related methods that are commonly used.
4. In the absence of source code can be used to fix bugs.
The use of category
In Obj-c, the method for declaring a category extension of an existing class is as follows:
@interface ClassName (CategoryName)
-methodname1
-methodname2
The above declarations are usually in the H file, and then we implement these methods in the M file:
@implementation ClassName (CategoryName)
-methodname1
-methodname2
We created an iOS single View applciation named Categoryexample. then expand the category for creating a NSString class. File->new->file then choose Cocoa Touch objective-c category. Named Reversensstring. The system automatically generates a fixed format classname+ CategoryName. h and. m files.
Statement category
Open the Nsstring+reversensstring.h file and add the following code inside:
#import <Foundation/Foundation.h>
@interface nsstring (reversensstring)
+ (nsstring*) ReverseString: ( nsstring*) strsrc;
Implement category
Implement ReverseString method in NSSTRING+REVERSENSSTRING.M file:
#import "Nsstring+reversensstring.h"
@implementationNSString (reversensstring)
+ (nsstring*) reversestring :(nsstring*) strsrc;
{
nsmutablestring *reversedstring =[[nsmutablestring alloc]init];
Nsinteger CharIndex = [strsrc length];
while (CharIndex > 0) {
charindex--;
Nsrange Substrrange =nsmakerange (CharIndex, 1);
[Reversedstring Appendstring:[strsrcsubstringwithrange:substrrange]];
}
return reversedstring;
}
The rest of the work is to verify our category, add a button reversestring to the view, and set the action method to reversestring. Add a label to the view, named MyString, and the default value is "Hellocategory design pattern!". Click the button to reverse the string. The main code is as follows:
-(Ibaction) ReverseString: (ID) Sender {
NSString *test = [Nsstringreversestring:_mystring.text];
_mystring.text = test;
Code Organization
Category is used for large class effective decomposition. Usually a method of a large class can be decomposed into different groups based on some logic or correlation, and the greater the amount of code in a class, the more useful it is to decompose the class into different files, each of which is a collection of some related methods of the class.
When multiple developers work together on a project, each person is responsible for the development and maintenance of a separate cagegory. This makes versioning easier because there are fewer work conflicts between developers.
Category vs Add Subclasses
There are no well-defined criteria for determining when to use category as a method of adding subclasses. However, there are several guiding recommendations:
1. If you need to add a new variable, you need to add a subclass.
2. If just add a new method, using category is a better choice.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.