So I have a singleton in Objective-C, there's one static method exposed to access the one and only instance:
+ (id)sharedMyClass {
static MyClass* sharedMyClass = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMyClass = [[self alloc] init];
sharedMyClass.myStringProperty = @"bla"; // here
});
return sharedMyClass;
}
and a non-exposed init method, used only when the instance is first created:
- (id)init {
if (self = [super init]) {
self.myStringProperty = @"bla"; // or here
return self;
}
return nil;
}
I can initialize the instance's properties in the first method, by setting them directly on sharedMyClass within the dispatch_once block. Or I can set them on self in the init method. I know functionally there isn't any difference, but is there a convention for this? What's the best practice?