• 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: Eric <email@hidden>
  • Date: Wed, 25 Sep 2002 02:14:35 -0400

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.

Hope this helps,
Eric Wang
_______________________________________________
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.

  • Follow-Ups:
    • Re: Making an object accessible by both MyDocument AND a custom class
      • From: Jérôme Laurens <email@hidden>
References: 
 >Making an object accessible by both MyDocument AND a custom class (From: Andrew Garber <email@hidden>)

  • Prev by Date: Re: multi user develloping
  • Next by Date: Re: [Q] Unlocking an arbitrary file?
  • Previous by thread: 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