I have a Singleton set up like this:
static Universe *instance;
+ (Universe *)instance { return instance; }
+ (void)initialize
{
static BOOL initialized = NO;
if(!initialized)
{
initialized = YES;
instance = [[Universe alloc] init];
}
}
- (id) init
{
self = [super init];
if (self != nil) {
self.showHistory = YES;
}
return self;
}
but now I realize that I'd like to instantiate it from Interface Builder. I was thinking of just cutting into the init
method like so
if (instance)
return instance;
is this a bad idea? I'd prefer IB to pick up the instance already created in the +initialize
method.
This can be done. There is a section about it in Cocoa Design Patterns by Buck and Yachtman.
In your case you could do something along the lines of:
static Universe *instance;
+ (Universe *)instance { return instance; }
+ (id)hiddenAlloc
{
return [super alloc];
}
+ (id)alloc
{
return [[self instance] retain];
}
+ (void)initialize
{
static BOOL initialized = NO;
if(!initialized)
{
initialized = YES;
instance = [[Universe hiddenAlloc] init];
}
}
- (id)init
{
if(instance==nil) // allow only to be called once
{
// your normal initialization here
}
return self;
}
The nib loading code will then correctly pick up the singleton via its call to [[Universe alloc] init]
, and you can still use instance
in your code as before.
The book has more detail and recommends implementing new
and allocWithZone
(both simply as return [self alloc];
), plus error-reporting stubs to catch copyWithZone
and mutableCopyWithZone
attempts for good measure.
That's going to leak. You can get away with it if you change it to:
if(instance) {
[self release];
return instance;
}
but it still smells a bit to me. I'm curious what use you have for singletons in IB; I suspect I would avoid this construct in my code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With