[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.
Step 1: Set Up Your Project
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."
Step 2: Create the Category
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."
Declare the Category Method
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
Implement the Category Method
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;}
Step 3: Import the Category
Click "ViewController.h" and import the category by adding the following code.
【yasi】中间竟然有个”+“号哦!
#import "NSString+RemoveNums.h"
Step 4: Test the Category
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);
Step 5: Use the Category Method
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.
Conclusion
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.
- [Objective-C] Categories 小例子
- Objective-C: Categories
- Categories in Objective-C
- Objective-C:Categories
- Objective-C 中Categories 初体验
- Objective-C categories in static library
- Objective-c的Categories和protocols
- Objective-C categories in static library
- Building Objective-C static libraries with categories
- objective-c的categories(分类)
- Building Objective-C static libraries with categories
- Ry’s Objective-C Tutorial---Categories
- Objective-C static libraries with categories
- Faking instance variables in Objective-C categories with Associative References
- Objective-C之类别(类扩展:Class Categories)
- Monkey-Patching iOS with Objective-C Categories Part III: Swizzling
- Effective Objective-C 2.0:Item 26: Avoid Properties in Categories
- 使用开源库 Objective-C RegEx Categories 处理正则表达式
- 锐评:手游安全破“黑”行动
- zBar扫描二维码时闪光灯自动打开问题
- 点双连通分量的求解 Tarjan算法的拓展
- C3P0数据库连接池的配置
- C# post数据时 出现如下错误: System.Net.WebException: 操作超时
- [Objective-C] Categories 小例子
- iOS UINavigationController跳转到指定页面
- 自定义标签
- java实习日记三
- [iOS]创建单一像素的分割线
- 视频截图一无编解码截图
- leetCode 3. Longest Substring Without Repeating Characters 解法与思路
- js返回星期方法
- linux升级相关