BNRItemStore
是一个单身人士,我很困惑为什么super allocWithZone:
必须调用而不是普通的旧的super alloc
。然后覆盖alloc
代替allocWithZone
.
#import "BNRItemStore.h"
@implementation BNRItemStore
+(BNRItemStore *)sharedStore {
static BNRItemStore *sharedStore = nil;
if (!sharedStore)
sharedStore = [[super allocWithZone: nil] init];
return sharedStore;
}
+(id)allocWithZone:(NSZone *)zone {
return [self sharedStore];
}
@end
[super alloc]
将致电至allocWithZone:
,您已覆盖它以执行其他操作。为了真正获得超类的实现allocWithZone:
(这是你想要的)而不是覆盖版本,你必须发送allocWithZone:
明确地。
The super
关键字代表相同的对象self
;它只是告诉方法调度机制开始在超类而不是当前类中寻找相应的方法。
Thus, [super alloc]
将进入超类,并在那里获取实现,如下所示:
+ (id) alloc
{
return [self allocWithZone:NULL];
}
Here, self
仍然代表您的自定义类,因此,您的重写allocWithZone:
运行,这将使您的程序进入无限循环。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)