• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: shared instance and nstableview
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: shared instance and nstableview
      • From: Yorh <email@hidden>
References: 
 >shared instance and nstableview (From: Yorh <email@hidden>)

  • Prev by Date: Re: shared instance and nstableview
  • Next by Date: Re: shared instance and nstableview
  • Previous by thread: Re: shared instance and nstableview
  • Next by thread: Re: shared instance and nstableview
  • Index(es):
    • Date
    • Thread