标签:
iOS开发—单例模式(ARC)
1 #import <Foundation/Foundation.h> 2 3 @interface YXAudioTool : NSObject 4 5 +(instancetype)shareAudioTool; 6 7 @end
1 #import "YXAudioTool.h" 2 3 @interface YXAudioTool() 4 5 //@property (nonatomic,strong) NSMutableDictionary * muscis; 6 7 @end 8 9 @implementation YXAudioTool 10 11 static id _instance; 12 13 - (instancetype)init 14 { 15 static dispatch_once_t onceToken; 16 dispatch_once(&onceToken, ^{ 17 if ((self == [super init])) { 18 //加载所需音乐资源 19 } 20 }); 21 return self; 22 } 23 24 +(instancetype)allocWithZone:(struct _NSZone *)zone{ 25 static dispatch_once_t onceToken; 26 dispatch_once(&onceToken, ^{ 27 _instance = [super allocWithZone:zone]; 28 }); 29 return _instance; 30 } 31 32 +(instancetype)shareAudioTool{ 33 static dispatch_once_t onceToken; 34 dispatch_once(&onceToken, ^{ 35 _instance = [[self alloc]init]; 36 }); 37 return _instance; 38 } 39 40 +(instancetype)copyWithZone:(struct _NSZone *)zone{ 41 return _instance; 42 } 43 44 +(instancetype)mutableCopyWithZone:(struct _NSZone *)zone{ 45 return _instance; 46 } 47 48 -(instancetype)copyWithZone:(struct _NSZone *)zone{ 49 return _instance; 50 } 51 52 -(instancetype)mutableCopyWithZone:(struct _NSZone *)zone{ 53 return _instance; 54 } 55 56 @end
1 #import "YXAudioTool.h" 2 3 @interface YXAudioTool() 4 5 //@property (nonatomic,strong) NSMutableDictionary * muscis; 6 7 @end 8 9 @implementation YXAudioTool 10 11 static id _instance; 12 13 - (instancetype)init 14 { 15 self = [super init]; 16 if (self) { 17 //加载所需音乐资源 18 } 19 return self; 20 } 21 22 +(instancetype)allocWithZone:(struct _NSZone *)zone{ 23 _instance = [super allocWithZone:zone]; 24 return _instance; 25 } 26 27 +(void)load{ 28 _instance = [[self alloc]init]; 29 } 30 31 +(instancetype)shareAudioTool{ 32 return _instance; 33 } 34 35 +(instancetype)copyWithZone:(struct _NSZone *)zone{ 36 return _instance; 37 } 38 39 +(instancetype)mutableCopyWithZone:(struct _NSZone *)zone{ 40 return _instance; 41 } 42 43 -(instancetype)copyWithZone:(struct _NSZone *)zone{ 44 return _instance; 45 } 46 47 -(instancetype)mutableCopyWithZone:(struct _NSZone *)zone{ 48 return _instance; 49 }
标签:
原文地址:http://www.cnblogs.com/tiankongdjiban/p/5578276.html