Re: Creating shared classes
Re: Creating shared classes
- Subject: Re: Creating shared classes
- From: Enrique Zamudio <email@hidden>
- Date: Wed, 18 Jul 2001 17:44:52 -0500
- Organization: Nasoft
You're doing it the wrong way (of course you know this, or you wouldn't
be posting to the list, right?)
You are assigning every new instance of UserSession to the
mainUserSession variable. And in your +sharedUserSession you're just
returning the variable. But calling that method (which is a class
method) will not create any instances. You should have it like this
instead:
@implementation UserSession
static UserSession *mainUserSession = nil;
- init
{ //does nothing really
[super init];
return self;
}
+ sharedUserSession
{
if (mainUserSession == nil)
mainUserSession = [[UserSession alloc] init];
return mainUserSession;
}
...
This way, the first time +sharedUserSession is called, it will create a
new instance of UserSession and store it in the mainUserSession is
variable (which is inside the class) and return it. On subsequent calls,
it only returns that object.
eZL