今天用到音效播放,顺便把单例做下记录
很多单例都会文章都会实现copyWithZone:
个人感觉不是很有必要(自定义的方法没有遵守NSCopying协议),我想不会有人给一个自定义对象发送copy消息吧,反正我不会,当然实现copyWithZone:
也不会有什么问题,起码真的有人直接copy单例对象,不至于奔溃。所以个人建议还是保留吧,防患于未然吧。
#import "JLBAudioTool.h"
static id _audioTool;
@interface JLBAudioTool()
@end
@implementation JLBAudioTool
/**
工具方法
*/
+ (instancetype)shareAudioTool
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_audioTool = [[self alloc] init];
});
return _audioTool;
}
/**
alloc方法内部其实就是调用该方法
*/
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_audioTool = [super allocWithZone:zone];
});
return _audioTool;
}
/**
防止有人发送copy消息给单例对象
*/
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
@end
网友评论