A singleton pattern is a class that creates only one instance of the entire program's life cycle.
Implementation process:
//Singclass.h#import<Foundation/Foundation.h>@interfaceSingclass:nsobject@property (nonatomic,strong) NSString*name;+ (ID) share_id;@end//SINGCLASS.M#import "Singclass.h"@implementationSingclass+ (ID) share_id{StaticSingclass *singclass =Nil; Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{Singclass=[[Singclass alloc]init]; }); returnSingclass;}@end
This method uses the time GCD technique, dispatch_once is the function that GCD provides, it executes only once in the code block's statement in the entire program life cycle, uses this kind of method to create the single-session automatic thread synchronization, is safe under the multithreading.
The purpose of using a singleton:
1, shared resources (properties)
2, sharing method
3, reduce multiple creation of classes (optimize performance)
System Single Example:
such as uiapplication, Nsfilemanager,nsbundle, Nsnotificationcenter, Nsuserdefaults.
This article Githu address Https://github.com/zhangkiwi/iOS_SN_Singclass
A single example of ios-design pattern