In iOS development, a singleton is one of the most useful design patterns. It is one of the most useful ways to share data between code without manually passing parameters. For a single case and other design patterns, refer to this book:
"Cocoa Design Pattern"
Background
The singleton is an important concept, it is an extremely convenient design pattern. The concept of Singleton is used extensively in the iphone SDK, for example, UIApplication's Sharedapplication method, which returns a uiapplication instance of the current application at any time.
How it will be implemented
Use the following code to implement a singleton class:
MyManager.h
#import <foundation/Foundation.h>
@interfaceMyManager: nsobject {
NSString *someproperty;
}
@property (nonatomic, retain) NSString *someproperty;
+ (ID) Sharedmanager;
@end
Mymanager.m
#import "MyManager.h"
@implementationMyManager
@synthesizesomeProperty;
#pragmamark Singleton Methods
+ (ID) Sharedmanager {
static Mymanager*sharedmymanager = nil;
Static Dispatch_once_toncetoken;
Dispatch_once (&oncetoken, ^{
Sharedmymanager = [[Self alloc] init];
});
return sharedmymanager;
}
-(ID) init {
if (self = [super init]) {
Someproperty =[[nsstring alloc] initwithstring:@ "Default property Value"];
}
return self;
}
-(void) Dealloc {
Should never is called, but justhere for clarity really.
}
@end
We define a static variable called Sharedmymanager, which is only instantiated once in the Sharedmanager method. With GCD's Dispath_once method, we ensure that the Sharedmymanager method is only created once. This is thread-safe and you don't have to worry about anything.
However, if you do not want to use GCG, you can also implement the Sharedmanager method as follows:
Non--GCD code
+ (ID) Sharedmanager {
@synchronized (self) {
if (Sharedmymanager = = nil)
Sharedmymanager = [[Self alloc] init];
}
Returnsharedmymanager;
}
This invokes the Singleton object:
Mymanager *sharedmanager = [Mymanager Sharedmanager];
In my code, this code is used in many places. I use these singleton objects to handle corelocation or CoreData.
Non-ARC code
If you do not use arc (not recommended), you should use the following code:
MyManager.h (non-ARC)
#import "MyManager.h"
static Mymanager *sharedmymanager = nil;
@implementation Mymanager
@synthesize Someproperty;
#pragma Mark Singleton Methods
+ (ID) Sharedmanager {
@synchronized (self) {
if (Sharedmymanager = = nil)
Sharedmymanager = [[Super Allocwithzone:null] init];
}
return sharedmymanager;
}
+ (ID) Allocwithzone: (Nszone *) Zone {
return [[Self sharedmanager]retain];
}
-(ID) Copywithzone: (Nszone *) Zone {
return self;
}
-(ID) retain {
return self;
}
-(unsigned) Retaincount {
return Uint_max; Denotes anobject that cannot be released
}
-(OneWay void) Release {
Never release
}
-(ID) autorelease {
return self;
}
-(ID) init {
if (self = [super init]) {
Someproperty = [[NSString alloc] initwithstring:@ "Default propertyvalue"];
}
return self;
}
-(void) Dealloc {
Should never is called,but just here for clarity really.
[Someproperty release];
[Super Dealloc];
}
@end