我学习创建Objective-C中一个单独的类和整个这个职位它使用GCD确保Singleton模式的强制执行来了。我对这个类中的实例init
方法以及它为什么在那里感到困惑。Objective-C的单例类的实例初始化函数
看起来像有人试图初始化MyManager
实例时会调用它,但为什么作者尝试初始化父类的实例([super init]
)?与ObjC混乱
#import "MyManager.h"
@implementation MyManager
@synthesize someProperty;
#pragma mark Singleton Methods
+ (id)sharedManager {
static MyManager *sharedMyManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMyManager = [[self alloc] init];
});
return sharedMyManager;
}
- (id)init {
//what is purpose of initialising parent class (NSObject's) instance
if (self = [super init]) {
someProperty = [[NSString alloc] initWithString:@"Default Property Value"];
}
return self;
}
- (void)dealloc {
// Should never be called, but just here for clarity really.
}
@end
http://stackoverflow.com/questions/2956943/why -should-I-调用自超init和http://stackoverflow.com/questions/18693023/objective-c-questions-about-self-super-init你的问题是不相关真的单身,因为我们几乎总是在自定义初始化方法中调用[super init]。 – Larme