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

2
  • What do you mean it doesn't working - what's not working? Commented Jul 27, 2011 at 9:27
  • @dean : MySingleton *mySingleton = [mySingleton sharedManager]; crash the program and show nothing in the Debugger console... Commented Jul 27, 2011 at 9:50

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 Don't be defensive against yourself coding a singleton with a dozen methods. This implementation is explained here: mikeash.com/pyblog/…
@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.
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.
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..

4 Comments

Thanks Gomathi, I implemented it and it still doesn't working (code updated)
Are u retaining the myToken string variable? I have edited my post.
Good catch! though you will probably want to copy instead of retain - see this answer for the reason why stackoverflow.com/questions/387959/…
(and you will want to check myToken != token first in case you accidentally dealloc the object that you are trying to keep)
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!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.