Using the taxonomy in Object-c is a compile-time tool that allows us to extend it by adding a method to a class (but not adding new instance variables through category), and we do not need to access the code in the class. This is a bit like using a stereotype in JavaScript to define attributes.
We can create a new method for a class without having to edit the class definition in code.
Here is the definition and use of the classification of the example program, through the following code, we can add to the object-c nsstring camelcasestring classification, using the Camelcasestring method, you can remove a string of spaces, and rewrite the words after the original space into uppercase (the string is converted to a hump).
#import <Foundation/Foundation.h>/* The process of defining a classification can be roughly divided into the following steps: First, create a new file with an interface, that is, create an existing class
The second step, in the new file to add the need to extend the method and the implementation of the method, that is, the need to add classification * *//nsstring represents the class name that will be added to the category, which must already exist.
CamelCase is the name of the method added for the class.
Only methods can be added and variables cannot be added.
Header file naming convention: classname+categoryname.h @interface NSString (CamelCase)-(nsstring*) camelcasestring; @end @implementation NSString (CamelCase)-(nsstring*) camelcasestring {//Call NSString internal method to get the hump
String.
Self points to the class that is added to the category.
NSString *castr = [self capitalizedstring];
Create an array to filter out spaces and combine characters with delimiters. Nsarray *array = [castr componentsseparatedbycharactersinset: [Nscharacterset whitespacecharact
Erset]];
The character output of the array is nsstring *output = @ "";
For (NSString *word in array) {output = [output STRINGBYAPPENDINGSTRING:WORD]; return output;
@end int main (int argc, const char * argv[]) {Nsautor
Eleasepool * Pool = [[NSAutoreleasePool alloc] init];
NSString *str = @ "My name is Bill.";
NSLog (@ "%@", str);
str = [STR camelcasestring];
NSLog (@ "%@", str);
[Pool drain];
return 0; }
This article is from the "Late Evening" blog, please be sure to keep this source http://yiluohuanghun.blog.51cto.com/3407300/1153613