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