[Objective-C] Categories 小例子

来源:互联网 发布:图书销售软件 编辑:程序博客网 时间:2024/05/22 03:46

转自:http://code.tutsplus.com/tutorials/objective-c-categories--mobile-10648


Categories provide the ability to add functionality to an object without subclassing or changing the actual object. A handy tool, they are often used to add methods to existing classes, such asNSString or your own custom objects.

Launch Xcode and click File > New > Project. Choose an iOS Single View Application from the window and click "Next." Name your product "Categories" and enter a name for your Company Identifier, such as "com.companyName.categories." Choose the iPhone device family and click "Next." Choose a location to store your project and click "Create."

Now that your project is set up, let's create a category that adds additional functionality to theNSString class. Click File > New > File and choose a Cocoa Touch Objective-C category from the window. Click "Next." Name your category "RemoveNums" and selectNSString from the "Category on" drop down menu (you may need to type this in manually). Click "Next" followed by "Create."

Back in your Xcode project, click "NSString+RemoveNums.h" to view the new category's header file. Add the following code to the interface to declare the method.

@interface NSString(RemoveNums)- (NSString*)removeNumbersFromString:(NSString*)string;@end

Click "NSString+RemoveNums.m" to view the category's implementation file. Add the following code to create a method that will remove all the numbers from anNSString. First we define anNSCharacterSet of the numbers zero through nine which we'll use as a reference to compare against the original input string. In this case, the original string "ABC 123" will have the numbers "123" removed from the string because the category method uses theNSString methodstringByTrimmingCharactersInSet:.

- (NSString*)removeNumbersFromString:(NSString*)string{    NSString*trimmedString =nil;    NSCharacterSet*numbersSet = [NSCharacterSetcharacterSetWithCharactersInString:@"0123456789"];    trimmedString = [stringstringByTrimmingCharactersInSet:numbersSet];    returntrimmedString;}

Click "ViewController.h" and import the category by adding the following code.

【yasi】中间竟然有个”+“号哦!

#import "NSString+RemoveNums.h"

Click "ViewController.m" and add the following code to the viewDidLoad method. The local variablestringWithNums contains a combination of letters and numbers. The next line takes the string variable and runs it through the category methodremoveNumbersFromString. Finally, NSLog outputs the returned value of the newly trimmed string without any numbers.

NSString *stringWithNums = @"ABC 123";NSLog(@"stringWithNums         --> %@",stringWithNums);stringWithNums = [stringWithNumsremoveNumbersFromString:stringWithNums];NSLog(@"trimmed stringWithNums --> %@",stringWithNums);

Click Product > Run, or click the "Run" arrow in the top left corner, to test the code. Notice the console shows the original input string, "ABC 123," as well as the string after it has been altered by the category method and the numbers have been removed.

Subclassing is one way to add functionality to an object, but avoiding unnecessary subclassing by using a category will help reduce the amount of code and keep your projects more organized. There are a number of scenarios where using a category is beneficial. 


0 0