8

I was wondering where is the best place to initialize members of the singleton class.

I'm using Apple fundamental guide singleton implementation. Could you please pinpoint at what line the inits happen? The code is the following:

static MyGizmoClass *sharedGizmoManager = nil;

+ (MyGizmoClass*)sharedManager
{
    @synchronized(self) {
        if (sharedGizmoManager == nil) {
            [[self alloc] init]; // assignment not done here
        }
    }
    return sharedGizmoManager;
}

+ (id)allocWithZone:(NSZone *)zone
{
    @synchronized(self) {
        if (sharedGizmoManager == nil) {
            sharedGizmoManager = [super allocWithZone:zone];
            return sharedGizmoManager;  // assignment and return on first allocation
        }
    }
    return nil; //on subsequent allocation attempts return nil
}

- (id)copyWithZone:(NSZone *)zone
{
    return self;
}

- (id)retain
{
    return self;
}

- (unsigned)retainCount
{
    return UINT_MAX;  //denotes an object that cannot be released
}

- (void)release
{
    //do nothing
}

- (id)autorelease
{
    return self;
}
Yohann T.
  • 1,915
  • 2
  • 23
  • 28
  • 2
    You might want to read http://boredzo.org/blog/archives/2009-06-17/doing-it-wrong. Do you really want a singleton that overrides release? That just masks bugs. – Jon Hess Aug 22 '09 at 00:26
  • And before you remind Jon Hess that you're following the Apple docs: I wrote that post specifically in response to the Apple docs. – Peter Hosey Aug 22 '09 at 02:01
  • Also worth noting is that classes do not have “members” of any kind. The closest you can get is a static variable in the class's implementation file. And class members aren't what you want to initialize anyway. What you meant to say is the *instance* variables of the singleton *instance*. – Peter Hosey Aug 22 '09 at 02:07
  • The right terminology would be: "Where should I put the initialization code of member variables of a singleton class". Then, once the singleton class has been instantiated at least one time, they becomes instance variables of the singleton instance !?!!? debate. – Yohann T. Aug 22 '09 at 11:59
  • Yohann: No. Objective-C classes do not have member variables. Only instances do. – Peter Hosey Aug 24 '09 at 22:16
  • See this [thread](http://stackoverflow.com/questions/355449/singleton-shared-data-source-in-objective-c) – Jordan Aug 22 '09 at 07:43

2 Answers2

18

It's as with usual classes - add this above the block:

-(id)init {
  if (self = [super init]) {
     // do init here
  }

  return self;
}

It will be called when singleton is accessed first time.

Rudi
  • 2,450
  • 5
  • 26
  • 37
  • if i create that init method, then it would accessible straight without passing through the sharedManager, right? Now if i make it private, that won't overide the init() method, right? – Yohann T. Aug 21 '09 at 23:59
  • Yes, it's accessible directly, but I don't think it should be - singleton will make sure it's called first time it's needed. You just call [[MySingletonClass sharedClass] message] as usual... – Rudi Aug 22 '09 at 00:07
  • +1, as noted by Jon Hess you generally should not overload all these methods unless you really absolutely have to ensure that there is one and only one instance of this object. That's pretty rare in practice. Generally you just want to make it easy to access a shared one, and for that you just need to implement a +sharedInstance (or +sharedManager, or whatever) method that returns a static instance, and not worry if a caller explicitly requests a unique instance. – Rob Napier Aug 22 '09 at 01:16
  • How are Apple developers doing it with their own singletons? There has to be a way to prevent users to access the init() method. – Yohann T. Aug 22 '09 at 11:52
  • Okay, you got to read the article posted on this page by Jon Hess, it basically explains it all. – Yohann T. Aug 22 '09 at 14:19
1

You can initialise them in the init method, like any other class.

However, be mindful that if your singleton contains member state, it may no longer be threadsafe. Since a singleton is accessible anywhere in the app at any time, it may be accessed from different threads.

Jasarien
  • 58,279
  • 31
  • 157
  • 188