I read many implementation of singleton pattern in Objective-C, and lots of the code contain such a line in the init
method:
if ((self = [super init])) {
...
}
And according to this page, which is referred by many questions regarding [super init]: https://www.cocoawithlove.com/2009/04/what-does-it-mean-when-you-assign-super.html
self = [super init]
"always returns the singleton instead of any subsequent allocation".
But why? The following is my implementation of a shared singleton
#pragma mark -
#pragma mark Constructors
+ (VoiceProfileManager*)sharedManager {
static VoiceProfileManager *sharedManager = nil;
//Executes a block object once and only once for the lifetime of an application.
static dispatch_once_t once;
dispatch_once(&once, ^{
sharedManager = [[VoiceProfileManager alloc] init];
});
return sharedManager;
}
- (id)init {
if ((self = [super init])) {
}
return self;
}
@end
Aucun commentaire:
Enregistrer un commentaire