0

I'm trying to run a singleton in my iPhone App'.

When I try to implement and to request it from the first class to the singleton's class, it's running but after, when I try to use it from an other class, it doesn't working...

Here's the call to the singleton in the first and second classes:

NSLog([ [MySingleton sharedMySingleton] getAuth]);

Her's the Singleton's class :

#import "MySingleton.h"

@implementation MySingleton

static MySingleton* _sharedMySingleton = nil;
@synthesize myToken;

+(MySingleton*)sharedMySingleton
{
    @synchronized([MySingleton class])
    {
        if (!_sharedMySingleton)
            [[self alloc] init];
        return _sharedMySingleton;
    }
    return nil;
}

+(id)alloc
{
    @synchronized([MySingleton class])
    {
        NSAssert(_sharedMySingleton == nil, @"Attempted to allocate a second instance of a singleton.");
        _sharedMySingleton = [super alloc];
        return _sharedMySingleton;
    }

    return nil;
}

-(id)init {
    self = [super init];
    if (self != nil) {
        myToken = [[NSString alloc] initWithString:@""];
    }

    return self;
}

-(void)setAuth:(NSString*) token {
    myToken=token;
}


-(NSString*)getAuth {
    return myToken;
}

- (id)copyWithZone:(NSZone *)zone {
    return self;
}
- (id)retain {
    return self;
}
- (unsigned)retainCount {
    return UINT_MAX; //denotes an object that cannot be released
}
- (void)release {
    // never release
}
- (id)autorelease {
    return self;
}
- (void)dealloc {
    // Should never be called, but just here for clarity really.
    [myToken release];
    [super dealloc];
}


@end

I imported correctly the singleton's class in the second class ;-)

That's it!

thanks for your help :-D

clement
  • 4,204
  • 10
  • 65
  • 133

3 Answers3

3

The following snippet has come direct from an Apple engineer and is currently regarded (in-house) as the best and most efficient way to implement a singleton (as it takes full advantage of GCD) ...

+(id)sharedInstance
{
    static dispatch_once_t pred = 0;
    static id object = nil;
    dispatch_once(&pred, ^{ object = /* object initialization goes here */ });
    return object;
}

Quote from said engineer regarding the above ...

Short, sweet, and whatever happens to be legal on the current architecture is likely to have been taken advantage of for the implementation, which means you never have to worry about what is or isn't legal in your current memory model.

It also allows you take get rid of all the boilerplate code needed by other, out-of-date implementations.

  • +1 Don't be defensive against yourself coding a singleton with a dozen methods. This implementation is explained here: http://www.mikeash.com/pyblog/friday-qa-2009-09-18-intro-to-grand-central-dispatch-part-iv-odds-and-ends.html – Jano Jul 27 '11 at 10:24
  • @jano I'm not sure if this comment is directed at me, but I wasn't being defensive just instead providing an alternate solution that takes advantage of modern technologies such as GCD. I'm not against coding a singleton with a dozen methods, I just don't see why it's necessary when a single method will suffice. –  Jul 27 '11 at 11:20
  • Sorry it wasn't, I agree with you, I just commented because this should be the default implementation unless you have a reason to do it differently. – Jano Jul 27 '11 at 11:42
1

There's a perfect topic on stackoverflow about singletons. I don't think even that I should make any other suggestions from myself. Just inspect this:

What should my Objective-C singleton look like?

It must be really more than enough for you. Good luck!

Community
  • 1
  • 1
makaron
  • 1,585
  • 2
  • 16
  • 30
1

Implemet the rest of the methods also as given here and try.

Edit:

Are u retaining the myToken string variable?

-(void)setAuth:(NSString*) token {
    if( token != myToken )
    {
       [myToken release];
       myToken=[token copy];
    }
}

Replace the setAuth method like this..

Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
  • Thanks Gomathi, I implemented it and it still doesn't working (code updated) – clement Jul 27 '11 at 09:23
  • Are u retaining the myToken string variable? I have edited my post. – Ilanchezhian Jul 27 '11 at 10:14
  • Good catch! though you will probably want to `copy` instead of `retain` - see this answer for the reason why http://stackoverflow.com/questions/387959/nsstring-property-copy-or-retain/388002#388002 – deanWombourne Jul 27 '11 at 10:39
  • (and you will want to check `myToken != token` first in case you accidentally dealloc the object that you are trying to keep) – deanWombourne Jul 27 '11 at 10:41