• 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: Yorh <email@hidden>
  • Date: Wed, 10 May 2006 09:33:41 +0200

Hi Andy and thanks,

So what do you think have I to do?
I know that the double if is redundant, but when i call sharedList = [self retain] i thought the sharedList variable was the same cause I called the retain class method.
Anyway all the examples in Apple's docs and cocoadev.com use this method and for me is the first time I use it.


thank you very much
Yorh

On 10/mag/06, at 04:40, Andy Lee wrote:

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:
40metagraphics.it


This email sent to email@hidden

_______________________________________________ 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
References: 
 >shared instance and nstableview (From: Yorh <email@hidden>)
 >Re: shared instance and nstableview (From: Andy Lee <email@hidden>)

  • Prev by Date: Re: Quit menu item undimming itself?!
  • Next by Date: Re: Menu Bar Application
  • Previous by thread: Re: shared instance and nstableview
  • Next by thread: Re: shared instance and nstableview
  • Index(es):
    • Date
    • Thread