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

'Pseudo' Singleton in Objective C


  • Subject: 'Pseudo' Singleton in Objective C
  • From: Pax <email@hidden>
  • Date: Thu, 14 Mar 2013 18:22:09 +0000

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


  • Follow-Ups:
    • Re: 'Pseudo' Singleton in Objective C
      • From: Oliver Fuchs <email@hidden>
    • Re: 'Pseudo' Singleton in Objective C
      • From: Seth Willits <email@hidden>
    • Re: 'Pseudo' Singleton in Objective C
      • From: Alex Zavatone <email@hidden>
    • Re: 'Pseudo' Singleton in Objective C
      • From: Jens Alfke <email@hidden>
  • Prev by Date: CoreData: sorting on a computed attribute
  • Next by Date: Re: Snapping windows while moving
  • Previous by thread: Re: CoreData: sorting on a computed attribute
  • Next by thread: Re: 'Pseudo' Singleton in Objective C
  • Index(es):
    • Date
    • Thread