The Singleton mode is often used in iOS development. Apple has provided the following official methods to use objective c singleton:
static MyGizmoClass *sharedGizmoManager = nil; + (MyGizmoClass*)sharedManager{ @synchronized(self) { if (sharedGizmoManager == nil) { [[self alloc] init]; // assignment not done here } } return sharedGizmoManager;} + (id)allocWithZone:(NSZone *)zone{ @synchronized(self) { if (sharedGizmoManager == nil) { sharedGizmoManager = [super allocWithZone:zone]; return sharedGizmoManager; // assignment and return on first allocation } } return nil; //on subsequent allocation attempts return nil} - (id)copyWithZone:(NSZone *)zone{ return self;} - (id)retain{ return self;} - (unsigned)retainCount{ return UINT_MAX; //denotes an object that cannot be released} - (void)release{ //do nothing} - (id)autorelease{ return self;}
Now, the ARC technology is widely used after iOS5. It turns out that this method is cumbersome and release is no longer used.
We can use GCD to implement the singleton mode:
+ (id)sharedInstance{ static dispatch_once_t pred = 0; __strong static id _sharedObject = nil; dispatch_once(&pred, ^{ _sharedObject = [[self alloc] init]; // or some other init method }); return _sharedObject;}
When dispatch_once is used, this method means that during the application lifecycle, this method is only executed once. This is the code of a singleton combined with GCD under ARC.
Because we may need multiple Singleton classes, it is troublesome to write them once, which makes it easier to write the code into a macro, you only need to input a class name to create a singleton class.
Create the name of the singleton method in the header file by passing in the class name:
#define DEFINE_SINGLETON_FOR_HEADER(className) \\+ (className *)shared##className;
How to Create a singleton by passing in a class name
#define DEFINE_SINGLETON_FOR_CLASS(className) \\+ (className *)shared##className { \static className *shared##className = nil; \static dispatch_once_t onceToken; \dispatch_once(&onceToken, ^{ \shared##className = [[self alloc] init]; \}); \return shared##className; \}
Use macros:
A new class testSingleton is created in the project, and there are. h and. m files.
. H
Code in. m:
#import "testSingleton.h"@implementation testSingletonDEFINE_SINGLETON_FOR_CLASS(testSingleton)@end
#define DEFINE_SINGLETON_FOR_HEADER(className) \\+ (className *)shared##className;#define DEFINE_SINGLETON_FOR_CLASS(className) \\+ (className *)shared##className { \static className *shared##className = nil; \static dispatch_once_t onceToken; \dispatch_once(&onceToken, ^{ \shared##className = [[self alloc] init]; \}); \return shared##className; \}#import <Foundation/Foundation.h>@interface testSingleton : NSObjectDEFINE_SINGLETON_FOR_HEADER(testSingleton);@end
You can put the macro in the project management macro file. This is to facilitate the demonstration and put it in the testSingleton. h file.
Obtain a ticket:
- (void)test{ testSingleton *testShare = [testSingleton sharedtestSingleton];}
If you have any questions, leave a message.
Rong Fangzhi (http://blog.csdn.net/totogo2010)
This article follows the "signature-non-commercial use-consistency" creation public agreement