iOS category
Category 基本介绍(如NSString):
- 文件名:
NSString+TestCategory.h NSString+TestCategory.m
h文件内容:
#import <Foundation/Foundation.h>
@interface NSString (TestCategory)
-(void)testCategoryMethod;
@end
m文件内容:
#import "NSString+TestCategory.h"
@implementation NSString (TestCategory)
-(void)testCategoryMethod{
NSLog(@"this is category method !");
}
@end
category与extends:
如果需要添加成员变量,则需要extends,category只能添加方法。个人认为category 是用于 对现有的一些代码进行扩展,而且用到的地方比较少。 另外category 只能给一个类添加新的方法, 而不能重写已有的方法。如果重写的话,会先调原来类的方法再调category里面的方法。