IOS 单例模式
单例模式:
1.永远只分配一块内存来创建对象
2.提供一个类方法, 返回内部唯一的一个对象(一个实例)
3.最好保证init方法也只初始化一次
单例模式 :定义一份变量(整个程序运行过程中, 只有1份)
//static id _instance;
ARC 与非ARC 控制内存内存
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// ## : 连接字符串和参数 #define singleton_h(name) + (instancetype)shared##name; #if __has_feature(objc_arc) // ARC #define singleton_m(name) \ static id _instance; \ + (id)allocWithZone:(struct _NSZone *)zone \ { \ static dispatch_once_t onceToken; \ dispatch_once(&onceToken, ^{ \ _instance = [super allocWithZone:zone]; \ }); \ return _instance; \ } \ \ + (instancetype)shared##name \ { \ static dispatch_once_t onceToken; \ dispatch_once(&onceToken, ^{ \ _instance = [[self alloc] init]; \ }) return _instance; \ } \ + (id)copyWithZone:(struct _NSZone *)zone \ { \ return _instance; \ } #else // 非ARC #define singleton_m(name) \ static id _instance; \ + (id)allocWithZone:(struct _NSZone *)zone \ { \ static dispatch_once_t onceToken; \ dispatch_once(&onceToken, ^{ \ _instance = [super allocWithZone:zone]; \ }); \ return _instance; \ } \ \ + (instancetype)shared##name \ { \ static dispatch_once_t onceToken; \ dispatch_once(&onceToken, ^{ \ _instance = [[self alloc] init]; \ }); \ return _instance; \ } \ \ - (oneway void)release \ { \ \ } \ \ - (id)autorelease \ { \ return _instance; \ } \ \ - (id)retain \ { \ return _instance; \ } \ \ - (NSUInteger)retainCount \ { \ return 1; \ } \ \ + (id)copyWithZone:(struct _NSZone *)zone \ { \ return _instance; \ } #endif
调用
#import "HMAudioTool.h" @interface HMAudioTool() @end @implementation HMAudioTool //// 定义一份变量(整个程序运行过程中, 只有1份) //static id _instance; - (id)init { if (self = [super init]) { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ // 加载资源 }); } return self; } singleton_m(AudioTool) ///** // * 重写这个方法 : 控制内存内存 // */ //+ (id)allocWithZone:(struct _NSZone *)zone //{ // // 里面的代码永远只执行1次 // static dispatch_once_t onceToken; // dispatch_once(&onceToken, ^{ // _instance = [super allocWithZone:zone]; // }); // // // 返回对象 // return _instance; //} // //+ (instancetype)sharedAudioTool //{ // // 里面的代码永远只执行1次 // static dispatch_once_t onceToken; // dispatch_once(&onceToken, ^{ // _instance = [[self alloc] init]; // }); // // // 返回对象 // return _instance; //} // //+ (id)copyWithZone:(struct _NSZone *)zone //{ // return _instance; //}
调用 HMAudioTool
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@interface HMViewController () @end @implementation HMViewController /** 单例模式: 1.永远只分配一块内存来创建对象 2.提供一个类方法, 返回内部唯一的一个对象(一个实例) 3.最好保证init方法也只初始化一次 */ - (void)viewDidLoad { [super viewDidLoad]; // NSObject *obj = [[NSObject alloc] init]; //#if ! __has_feature(objc_arc) // [obj release]; //#endif // // NSObject *obj1 = [[NSObject alloc] init]; //#if ! __has_feature(objc_arc) // [obj1 release]; //#endif // // NSObject *obj2 = [[NSObject alloc] init]; //#if ! __has_feature(objc_arc) // [obj2 release]; //#endif HMAudioTool *tool1 = [HMAudioTool sharedAudioTool]; HMAudioTool *tool2 = [HMAudioTool sharedAudioTool]; HMAudioTool *tool3 = [[HMAudioTool alloc] init]; HMAudioTool *tool4 = [[HMAudioTool alloc] init]; HMAudioTool *tool5 = [[HMAudioTool alloc] init]; HMAudioTool *tool6 = [[HMAudioTool alloc] init]; // [tool1 play:@"win.mp3"]; NSLog(@"%p %p %p %p", tool1, tool2, tool3, tool4); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end