在Objective-C中,对于NSString、NSArray等一些系统的基础类,苹果公司不推荐继承创建新类的,要想给它们扩展功能只能给它们添加新的categroy(类目)。
但是直接继承的话,也是可行的,只是会出现各种问题
以NSString为例
首先,创建一个NSString的子类 ExtensionString,不添加任何方法和属性
直接创建一个ExtensionString的实例对象,是没有问题的
ExtensionString *str = [[ExtensionString alloc]init];
但是如果使用其他初始化方法,比如
ExtensionString *str = [[ExtensionString alloc]initWithString:@"abcd"];
运行时会提示如下错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** initialization method -initWithCharactersNoCopy:length:freeWhenDone: cannot be sent to an abstract object of class ExtensionString: Create a concrete instance!'
也就是初始化时,调用的initWithCharactersNoCopy:length:freeWhenDone:方法存在问题
看一下苹果官方文档中,有这样一段:
It is possible to subclass NSString (and NSMutableString), but doing so requires providing storage facilities for the string (which is not inherited by subclasses) and implementing two primitive methods.
首先是需要为子类的字符串提供存储机制,其次还要实现两个方法
文档中继续指明了这两个方法:
Any subclass of NSString must override the primitive instance methods length and characterAtIndex:. These methods must operate on the backing store that you provide for the characters of the string. For this backing store you can use a static array, a dynamically allocated buffer, a standard NSString object, or some other data type or mechanism.
同时,也说明了要为string中的字符提供存储机制,这些存储机制可以是静态数组、动态分配的缓存、NSString的对象或者是其他数据类型。
那么,问题就可以这样解决:
首先,在子类中声明一个存储字符串的属性:
@interface ExtensionString : NSString
{
NSString *_backingStore;
}
@end
然后在实现方法中,覆写以上两个方法length和characterAtIndex:,当然也要覆写一下初始化方法
@implementation ExtensionString
- (id)initWithString:(NSString *)aString
{
if (self = [self init]) {
_backingStore = [[NSString stringWithString:aString] copy];
}
return self;
}
- (NSUInteger)length
{
return [_backingStore length];
}
- (unichar)characterAtIndex:(NSUInteger)index
{
return [_backingStore characterAtIndex:index];
}
@end
将字符串存储在_backingStore之中,然后通过读取该字符串来获得子类字符串的长度和字符
现在,继承NSString的这个子类就没有问题了。
这个做法同样适用于NSArray、NSDictionary、NSNumber等