Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Shared Instance of a class
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Shared Instance of a class



I am aware of this (took me a heck of a while to figure out though!). The class in question is a subclass of NSObject so it just uses the plain init method at the moment.

Mike.

On 19 May 2006, at 21:42PM, Christopher Hickman wrote:

Careful. If its in a nib, it may not receive an init.  I ran into this
before, and had to put my init code in awakeFromNib.

I'll just quote someone more knowledgeable:

On 5/3/06, Ondra Cada wrote:
IB doesn't init it when it is instantiated? I didn't realize.

That's a difficult area. Of course all the objects *are* init'd, but the concrete init method used changes (at the first look, at least :)) in a totally random way :) It tends to be initWithCoder: often, but not always, depending on the NIB layout details which you don't want to learn actually (I did, and forgot happily long ago :)).

-----Original Message-----
From: cocoa-dev-bounces+tophu=email@hidden
[mailto:cocoa-dev-bounces+tophu=email@hidden] On
Behalf Of Mike Abdullah
Sent: Friday, May 19, 2006 3:46 PM
To: Tommy Nordgren
Cc: email@hidden
Subject: Re: Shared Instance of a class

Cheers guys.  I hadn't realised that you could use static outside of
a method.  This way I can set the value in my object's init method :)

Mike.

On 19 May 2006, at 19:41PM, Tommy Nordgren wrote:


On 19 maj 2006, at 19.42, Mike Abdullah wrote:

I have a custom class that I instanciate in my MainMenu
nib. What
I want to do is be able to issue the command:

[MyClass sharedInstance]

and get that particular instance of the class.  I know how to do
this with a class that is only instanciated once the
sharedInstance message is received, but the object pre-exists.

NSDocumentController is capable of this.  Any ideas?

Mike.
_______________________________________________
In your implementation file have a static pointer to
your class.
Assign self to it in your awakeFromNib method.
Now you can have a class method that returns the shared instance,
or throws an exception if it's not defined.
------------------------------------------------------
"Home is not where you are born, but where your heart finds peace" -
Tommy Nordgren, "The dying old crone"
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



_______________________________________________ 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: 
 >RE: Shared Instance of a class (From: "Christopher Hickman" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.