• 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: Binding to a Singleton
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Binding to a Singleton


  • Subject: Re: Binding to a Singleton
  • From: Shaun Wexler <email@hidden>
  • Date: Tue, 26 Sep 2006 09:49:31 -0700

On Sep 26, 2006, at 9:02 AM, Mike Abdullah wrote:

Because it's a singleton. I want to bind to it from my document NIB, and by instantiating the class in the NIB will result in multiple instances of my class. There will be one instance for every document - not something I want!

In my app I have a singleton class that behaves kinda like NSUserDefaults. So I'm wondering, what's my best bet for how to go about binding to this class from a document NIB.

A nib file contains archived copies of your objects, which are instantiated in the app using -initWithCoder: and that ultimately calls -init on your object. Your singleton class should override +allocWithZone: to prevent the creation of additional objects. Your singleton instance can then intercept either of the init methods as well as -awakeFromNib, and deal with the nib accordingly. You do not want to use the singleton as File's Owner for outlet or binding purposes though.


Another way is to declare a category and bind to NSApplication.mySingleton:

static MySingletonClass *mySingletonSharedInstance;

@interface NSApplication (MySingletonClass)

- (MySingletonClass *)mySingleton;

@end

@implementation NSApplication (MySingletonClass)

+ (void)load
{
    [MySingletonClass class]; // +initialize if necessary
}

- (MySingletonClass *)mySingleton
{
    return mySingletonSharedInstance;
}

@end

@implementation MySingletonClass

+ (void)initialize
{
    if (self == [MySingletonClass class]) {
	mySingletonSharedInstance = [[super allocWithZone:NULL] init];
    }
}

+ (id)allocWithZone:(NSZone *)zone
{
    return nil;
}

@end

--
Shaun Wexler
MacFOH
http://www.macfoh.com


_______________________________________________ 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: 
 >Binding to a Singleton (From: Mike Abdullah <email@hidden>)
 >Re: Binding to a Singleton (From: Andrei Tchijov <email@hidden>)
 >Re: Binding to a Singleton (From: Mike Abdullah <email@hidden>)

  • Prev by Date: Re: Problem with relationships in Core Data
  • Next by Date: Re: Problem with relationships in Core Data
  • Previous by thread: Re: Re: Binding to a Singleton
  • Next by thread: Questions About Core Image Filters
  • Index(es):
    • Date
    • Thread