在苹果文档单例中:
static MyGizmoClass *sharedGizmoManager = nil;
+ (MyGizmoClass*)sharedManager {
if (sharedGizmoManager == nil) {
sharedGizmoManager = [[super allocWithZone:NULL] init];
}
return sharedGizmoManager;
}
+ (id)allocWithZone:(NSZone *)zone {
return [[self sharedManager] retain];
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)retain {
return self;
}
- (NSUInteger)retainCount {
return NSUIntegerMax; //denotes an object that cannot be released
}
- (void)release {
//do nothing
}
- (id)autorelease {
return self;
}
我有个问题:
+ (id)allocWithZone:(NSZone *)zone {
return [[self sharedManager] retain];
}
保留应该省略,但为什么要保留保留?
因为 return [self sharedManager]
或[[self sharedManager] retain]
等于。