Re: shared instance and nstableview
Re: shared instance and nstableview
- Subject: Re: shared instance and nstableview
- From: Andy Lee <email@hidden>
- Date: Tue, 9 May 2006 22:40:02 -0400
On May 9, 2006, at 6:57 PM, Yorh wrote:
+(GAChannelList*)sharedInstance{
static GAChannelList * sharedList = nil;
if (sharedList == nil) {
sharedList = [[GAChannelList alloc] init];
}
return sharedList;
}
- (id)init{
static GAChannelList * sharedList = nil;
if (self = [super init]) {
if (self){
sharedList = [self retain];
allData = [[NSMutableArray alloc]init];
}
}
return self;
}
The sharedList in your -sharedInstance method is not the same
variable as the sharedList in your -init method. They are two
separate variables with the same name, and the same lifetime (i.e.,
global lifetime), and different scopes (one can only be referred to
in the -sharedInstance method, and the other can only be referred to
in the -init method). You might want to read up on static variables
in C.
Also, in your -init method, the "if (self)" is redundant, because you
have already tested the value of self. You should understand what
the "if ()" does:
* "[super init]" invokes the inherited (super) implementation of
-init, which returns an object
* "self = [super init]" assigns that object to the variable
"self" (note the single = for assignment, as opposed to double == for
comparison)
* every expression in C evaluates to something; in this case,
the assignment expression "self = ..." evaluates to the value that
was assigned
* the "if (...)" tests whether the value inside the parentheses
is 0 or non-zero; since nil has the value zero, this is the same as
testing whether self is nil
The page Lawrence pointed you to should put you on the right track to
creating a singleton instance. But you should understand what your C
code is really doing.
--Andy
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden