Category in objective-C)
1. Category concept: dynamically add new behaviors (methods) for existing classes)
2. How to Create a category
(1) Use xcode to generate a category
(2) You can manually generate a category and generate two @ interfaces in the header file.
For example:
In student. in the hfile, @ interface Student: nsobject-(void) test1; @ end is the class of student, and test is the name of the class @ interface student (TEST)-(void) Test2; @ end in student. in the M file, @ implementation student-(void) test1 {nslog (@ "test1 ...");} @ End @ implementation student (TEST)-(void) Test2 {nslog (@ "Test2 ...");} @ End
3 Category Description
(1) () represents a classification
(2) test in () represents the name of the category.
(3) classification can only be extended, and member variables cannot be added.
(4) No subclass is required for class extension using category
CATEGORY uses a simple method to modularize the class-related methods and distributes different class methods to different classification files.
4. Use Cases of category
(1) You may want to add new methods to one or more classes when defining a class (for example, a requirement change ).
(2) A class contains many different types of methods that need to be implemented, and these methods need to be implemented by members of different teams.
(3) when using the basic class library, you may want these classes to implement some of the methods you need, such as nsstring + JSON. h, to expand some JSON parsing methods for the nsstring class.