A category is a way to add useful functionality or methods to a class that can be a system class, a custom class
Categories are not the same as subclasses, categories can only add some method property variables or not add
You can extend an existing class without creating a new class
Working with other programmers will work better when doing the project.
The diagram for the new category is placed on the last side
We add a printing method for the system class Nsarray to enable the output of Chinese
The interface is as follows
And then declare a function in Nsarray+mynslog.h
#import <Foundation/Foundation.h> @interface Nsarray (Mynslog)-(NSString *) Printchinese; @end
And then nsarray+mynslog.m.
#import "Nsarray+mynslog.h" @implementation Nsarray (Mynslog)-(NSString *) printchinese{ nsmutablestring * string = [[Nsmutablestring alloc] init]; [string appendstring:@ "{\ n"]; We are a bit system class Nsarray add a function that can print out Chinese (the following self refers to the array we want to output //That is, to output the Chinese array for (int i = 0; i < Self.count; i++) { // The string can be output in Chinese [string appendformat:@ "\t%@,\n", Self[i]]; } [String appendformat:@ "}"]; return string;} @end
In main.m, you only need to introduce a category header file, and if it is a custom class, simply enter the header file of the category.
#import <Foundation/Foundation.h> #import "nsarray+mynslog.h" int main (int argc, const char * argv[]) { @ Autoreleasepool { Nsarray * array = @[@ "I am", @ "snail", @ "Snail"]; The output of the system is garbled/ * ( "\u6211\u662f", "\u8717\u725b", Snail ) */ NSLog (@ "%@", Array); When we use the added function output the Chinese can output to /* { I am, snail, Snail, } * /NSLog (@ "%@", [array Printchinese]); } return 0;}
Here is a diagram of the new category attached
First of all, the new is suitable for selecting the following file
Next, then write your own defined category name in the first line the second line is which class you want to add the category to
Then save it next, and you'll see that you've created two files of a similar class
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
SNAIL-OC Type of study category