• 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: 'Pseudo' Singleton in Objective C
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 'Pseudo' Singleton in Objective C


  • Subject: Re: 'Pseudo' Singleton in Objective C
  • From: Oliver Fuchs <email@hidden>
  • Date: Thu, 14 Mar 2013 19:43:38 +0100

Hi,

Firstly, I'm assuming the more information button is the only location
which triggers displaying/redisplaying.

if thats the case I would just store the instance of your class in a property and make sure it will be created only once.

You can accomplish this by simply overwriting the getter method like this:

-(your class) foo {
 if (!_foo) {
    _foo = [[yourclass alloc]init];
}
return _foo;
}

be careful about mem managment. above is good for a retain property!

best regards,
oliver


Von meinem iPhone gesendet

Am 14.03.2013 um 19:22 schrieb Pax <email@hidden>:

> I don't really know how to describe what I'm trying to do except as a 'Pseudo' Singleton.  I have a class with an NSWindow, which displays information.  It is run by selecting an NSMenuItem called 'More Information…'
>
> My issue is that I only want one instance of the Information class to be loaded at a given time (and therefore only one information window on screen at a given time).  If the information window is already loaded and it is then reselected from the menu then the existing window needs to be brought to the front - nothing more.
>
> I have currently resolved it by making my class a singleton - but that's not the right answer because it means that I can't release information window and its class when it gets closed, resulting in a minor leak (only minor because, as a singleton, the instance is only ever loaded once anyway).  Even so, this is one leak too many - and I'be grateful if someone could tell me the correct way to do what I am trying to do.
>
> Currently, I am setting to be a singleton as follows:
>
> static informationWindow *singletonManager = nil;
>
> @implementation informationWindow
>
> //Code to handle singleton
> + (id)singleton
> {
>    @synchronized(self)
>    {
>        if(singletonManager == nil)
>            singletonManager = [[super allocWithZone:NULL] init];
>    }
>    return singletonManager;
> }
>
> + (id)allocWithZone:(NSZone *)zone
> {
>    return [[self singleton] retain];
> }
>
> - (id)copyWithZone:(NSZone *)zone
> {
>    return self;
> }
>
> - (id)retain
> {
>    return self;
> }
>
> - (unsigned)retainCount
> {
>    return UINT_MAX;
> }
>
> - (oneway void)release
> {
>
> }
>
> - (id)autorelease
> {
>    return self;
> }
>
>
> I'm not using ARC.  All suggestions gratefully received!
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


References: 
 >'Pseudo' Singleton in Objective C (From: Pax <email@hidden>)

  • Prev by Date: Re: Another Gnarly Objective-C Question!
  • Next by Date: Re: 'Pseudo' Singleton in Objective C
  • Previous by thread: Re: 'Pseudo' Singleton in Objective C
  • Next by thread: forward decls vs #import
  • Index(es):
    • Date
    • Thread