1 @interface HMMusicTool : NSObject 2 + (instancetype)sharedMusicTool; 3 @end 4 5 @implementation HMMusicTool 6 static id _instance; 7 8 /** 9 * alloc方法内部会调用这个方法 10 */ 11 + (id)allocWithZone:(struct _NSZone *)zone 12 { 13 if (_instance == nil) { // 防止频繁加锁 14 @synchronized(self) { 15 if (_instance == nil) { // 防止创建多次 16 _instance = [super allocWithZone:zone]; 17 } 18 } 19 } 20 return _instance; 21 } 22 23 + (instancetype)sharedMusicTool 24 { 25 if (_instance == nil) { // 防止频繁加锁 26 @synchronized(self) { 27 if (_instance == nil) { // 防止创建多次 28 _instance = [[self alloc] init]; 29 } 30 } 31 } 32 return _instance; 33 } 34 35 - (id)copyWithZone:(NSZone *)zone 36 { 37 return _instance; 38 } 39 @end