#if __has_feature(obj_arc)
// add ARC code
#else
// add MRC code
#endif
static id _instance;
- (id)init
{
if (self = [super init]) {
// 加载资源
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
});
}
return self;
}
// alloc 方法内部会调用这个方法,这里才会进行分配内存的实质操作
+ (id)allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [super allocWithZone:zone];
});
return _instance;
}
+ (instancetype)sharedClassName
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[self alloc] init];
})
return _instance;
}
+ (id)copyWithZone:(struct _NSZone *)zone
{
return _instance;
}
static id _instance;
- (id)init
{
if (self = [super init]) {
// 加载资源
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
});
}
return self;
}
+ (id)allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [super allocWithZone:zone];
});
return _instance;
}
+ (instancetype)sharedClassName
{
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;
}
static id _instance;
+ (id)allocWithZone:(struct _NSZone *)zone
{
@synchronized(self) {
if (!_instance) {
_instance = [super allocWithZone:zone];
}
}
return _instance;
}
+ (instancetype)sharedSoundTool
{
@synchronized(self) {
if (!_instance) {
_instance = [[self alloc] init];
}
}
return _instance;
}
+ (id)copyWithZone:(struct _NSZone *)zone
{
return _instance;
}
- (id)retain { return self; }
- (NSUInteger)retainCount { return 1; }
- (oneway void)release {}
- (id)autorelease { return self; }