So it has been my understanding that using init within a method name is not advisable if that method doesn’t actualize initialize a new instance of the object. However, what is the case for a singleton type class? If I do something like this:
+ (MyClass*) sharedInstance {
__block MyClass *sharedInstance = nil;
static dispatch_once_t once_token;
dispatch_once(&once_token, ^{
sharedInstance = [[MyClass alloc] init];
});
return sharedInstance;
}
And then have another method:
- (void) initializeInstance {
// Do some stuff
// Never call the init method
}
Will there be extra retain cycles or other odd ARC behavior if I do this?
The method
-initializeInstanceis not a problem. This does not get classified into theinitfamily, so the compiler does not treat it specially. According to the documentation, in order to be classified into theinitfamily, it must meet the following rules:init, or must begin withinitfollowed by any character other than a lowercase letter.initializeInstancedoes not meet this rule.initmethod must return an Obj-C object.initializeInstancedoes not meet this rule either. I believe if this rule is violated that causes a compile-time error rather than simply not treating the method asinit.