views:

16

answers:

1

Hi,

I have an object which is initiated in my nib file. I want it to be a singleton but also accessible from code through [myClass sharedInstance];. Right now I have this:

static myClass *singleton = nil;

@implementation myClass
+ (myClass *)sharedInstance
{
    if (!singleton) singleton = [[self class] new];
    return singleton;
}

+ (id)alloc
{
    return [self sharedInstance];
}

- (id)init 
{ 
    if ([self class] != nil)
        self = [super init])
    return self; 
} 
@end

But alloc never gets called.

A: 

Solved. See the end of http://www.cocoadev.com/index.pl?SingletonDesignPattern

Jane