• 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: Making an object accessible by both MyDocument AND a custom class
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Making an object accessible by both MyDocument AND a custom class


  • Subject: Re: Making an object accessible by both MyDocument AND a custom class
  • From: Jérôme Laurens <email@hidden>
  • Date: Wed, 25 Sep 2002 12:55:30 +0200

Le mercredi, 25 sep 2002, ` 08:14 Europe/Zurich, Eric a icrit :

on 9/24/02 9:44 PM, Andrew Garber at email@hidden wrote:

Hi! I'm writing a cocoa document-based app. I have a subclass of
NSDocument called MyDocument (as Project Builder normally provides),
and a subclass of NSObject called MyCustomObject. I also have an
instance of an NSPanel in my MainMenu.nib which is connected to an
instance of MyPanelController (a subclass of NSWindowController). I
only want MyCustomObject object to be instantiated once. I want
instances of MyDocument to be able to message the instance of
MyCustomObject (without creating new instances of it). I also want the
instance of MyPanelController to be able to message the instance of
MyCustomObject.

How can I allow objects to communicate with the single instance of
MyCustomObject?


Hi Andrew,

I think what you want here is a "shared instance" method for your
MyCustomObject class. It would go something like this:


+ (MyCustomObject *) sharedMyCustomObject
{
static MyCustomObject *sharedMyCustomObject = nil;

if (sharedMyCustomObject == nil)
{
sharedMyCustomObject = [[MyCustomObject alloc] init];
}
return sharedMyCustomObject;
}

Note that the method is a class method, so whenever you need the shared
MyCustomObject instance, you would call the method like so: [MyCustomObject
sharedMyCustomObject]. This avoids instantiating multiple instances of
MyCustomObject.


this is not completely suitable for use in a nib, adopt instead the following design

@implementation MyObject
static MyObject * sharedInstance;
- (id) init;
{
if(sharedInstance)
{
[self dealloc];// do not leak
return [sharedInstance retain];// an init must increase the retain count by one: now [[[MyObject alloc] init] autorelease] is safe.
}
else if(self = [super init])
{
// do your init job here
return sharedInstance= self;
}
else
{
// manage the errors
return nil;
}
}
- (void) release;
{
if([self retainCount]==0)
sharedInstance = nil;// what was the shared instance is no more accessible
[super release];
return;
}
+ (id) sharedInstance;
{
return sharedInstance? sharedInstance: [[self alloc] init];
}
@end

if you instantiate MyObject from the nib, the init method will be called and the sharedInstance is returned (created the first time...)
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
References: 
 >Re: Making an object accessible by both MyDocument AND a custom class (From: Eric <email@hidden>)

  • Prev by Date: Spinning Progress Indicator
  • Next by Date: NSDrawer in full screen mode
  • Previous by thread: Re: Making an object accessible by both MyDocument AND a custom class
  • Next by thread: Re: Making an object accessible by both MyDocument AND a custom class
  • Index(es):
    • Date
    • Thread