Singletons in Objective-C
2013-12-20 15:44
309 查看
转载自http://www.galloway.me.uk/tutorials/singleton-classes/ (稍后过滤一下信息)
Singletons in Objective-C
One of my most used design patterns when developing for iOS is the singleton pattern. It’s an extremely powerful wayto share data between different parts of code without having to pass the data around manually. More about the singleton pattern and other patterns can be found in this excellent book:
Background
Singleton classes are an important concept to understand because they exhibit an extremely useful design pattern. This idea is used throughout the iPhone SDK, for example, UIApplication has a method called sharedApplication which when called from anywhere willreturn the UIApplication instance which relates to the currently running application.
How to implement
You can implement a singleton class in Objective-C using the following code:MyManager.h
1 2 3 4 5 6 7 8 9 10 11 | #import <foundation/Foundation.h> @interface MyManager : NSObject { NSString *someProperty; } @property (nonatomic, retain) NSString *someProperty; + (id)sharedManager; @end |
1 2 3 4 5 6 7 8 9 10 1112 | #import "MyManager.h" @implementation MyManager @synthesize someProperty; #pragma mark Singleton Methods + (id)sharedManager { static MyManager *sharedMyManager = nil; static dispatch_once_t onceToken; 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 be called, but just here for clarity really. } @end |
sharedMyManagerwhich
is then initialised once and only once in
sharedManager.
The way we ensure that it’s only created once is by using the
dispatch_oncemethod
from Grand Central Dispatch (GCD). This is
thread safe and handled entirely by the OS for you so that you don’t have to worry about it at all.
However, if you would rather not use GCD then you should use the following code for
sharedManager:
Non-GCD based code
1 2 3 4 5 6 7 8 | + (id)sharedManager { static MyManager *sharedMyManager = nil; @synchronized(self) { if (sharedMyManager == nil) sharedMyManager = [[self alloc] init]; } return sharedMyManager; } |
MyManager *sharedManager = [MyManager sharedManager];
I’ve used this extensively throughout my code for things such as creating a singleton to handle CoreLocation or CoreData functions.
Non-ARC code
Not that I recommend it, but if you are not using Automatic Reference Counting (ARC), then you should usethe following code:
MyManager.h non-ARC
1 2 3 4 5 6 7 8 9 10 1112 | #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 an object that cannot be released } - (oneway void)release { // never release } - (id)autorelease { return self; } - (id)init { if (self = [super init]) { someProperty = [[NSString alloc] initWithString:@"Default Property Value"]; } return self; } - (void)dealloc { // Should never be called, but just here for clarity really. [someProperty release]; [super dealloc]; } @end [/code] |
相关文章推荐
- Singleton Pattern in Objective-C
- Singletons in Objective-C
- Singletons in Objective-C
- Swift and Objective-C in the Same Project
- 单件模式在C#中的实现--Implementing the Singleton Pattern in C#
- Implementing the Singleton Pattern in C#
- Delegation in Objective-C
- Programming in Objective-C 学习笔记09——对象复制与归档
- ARC forbids Objective-C objects in struct
- An unexpected version directory `Objective-C` was encountered for the'<xxx>'Pod in the `xxx`
- Creating Protected and Private Variables in Objective-C Class
- Objective —C 单例模式 Singleton
- Objective-C中的单例模式(singleton)
- #define vs const in Objective-C 用 #define 还是 const
- Using Properties And Synthesize In Objective-C 2.0 For Getters And Setters
- Thread safe Singleton in C#
- 28What are mutable and immutable types in Objective C?
- [iOS开发站在巨人肩膀上]之How to implement Queue in Objective-C
- Singletons in Cocoa, are they evil?
- objective-c &quot;_OBJC_CLASS_$_SKPSMTPMessage&quot; eferenced from: objc-class-ref in xxx.o symbol(s) not found for architect