public
Last active

Singleton.m

  • Download Gist
gistfile1.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#import "SingletonClass.h"
#import "ViewController.h"
@implementation SingletonClass
@synthesize locationManager;
@synthesize startLocation,stopLocation;
@synthesize testStr;
//static SingletonClass *sharedManager = nil;
#pragma mark Singleton Methods
/*+ (SingletonClass *)sharedManager {
static SingletonClass *sharedManager = nil;
if (sharedManager == nil) {
sharedManager = [[super allocWithZone:NULL] init];
}
return sharedManager;
}*/
+(SingletonClass *)sharedManager
{
static SingletonClass *sharedManager = nil;
if (sharedManager !=nil)
{
return sharedManager;
}
static dispatch_once_t pred;
dispatch_once(&pred, ^{
sharedManager = [SingletonClass alloc];
sharedManager=[sharedManager init];
});
return sharedManager;
}
- (id)init
{
self = [super init];
if (self)
{
testStr = [[NSString alloc] initWithString:@"String from Singleton Class, Hello!"];
}
return self;
}
-(void)dealloc
{
// I'm never called!
[locationManager release];
[stopLocation release];
[startLocation release];
[testStr release];
[super dealloc];
}
+ (id)allocWithZone:(NSZone*)zone {
return [[self sharedManager] retain];
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)retain {
return self;
}
- (NSUInteger)retainCount {
return NSUIntegerMax;
}
- (oneway void)release {
}
- (id)autorelease {
return self;
}
@end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.