Re: Shared Instance of a class
Re: Shared Instance of a class
- Subject: Re: Shared Instance of a class
- From: "Dennis C. De Mars" <email@hidden>
- Date: Sat, 20 May 2006 13:26:31 -0700
One possible simple solution: I assume this is a singleton class
(i.e. the one shared instance is the only instance of the class). You
could create the shared instance in the class' +initialize routine.
Have +initialize check to see if the shared object has been created
already (or arrange to have +initialize called only once, an issue if
you make subclasses of this class) and create it there.
+initialize is called the first time an instance method of the class
is called, so it is guaranteed to execute even if the first method
called is something other than -init.
- Dennis D.
On May 19, 2006, at 3:03 PM, Mike Abdullah wrote:
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:
40fractaldomains.com
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