/**
static : 修饰变量
1> 修饰全局变量
* 全局变量的作用域仅限于当前文件内部
2> 修饰局部变量 :
* 局部变量的生命周期 跟 全局变量 类似
* 但是不能改变作用域
* 能保证局部变量永远只初始化1次,在程序运行过程中,永远只有1分内存
*/
1.ARC
#import "HMMusicTool.h"
@implementation HMMusicTool
static id _instance;
/**
* alloc方法内部会调用这个方法
*/
+ (id)allocWithZone:(struct _NSZone *)zone
{
if (_instance == nil) { // 防止频繁加锁
@synchronized(self) {
if (_instance == nil) { // 防止创建多次
_instance = [super allocWithZone:zone];
}
}
}
return _instance;
}
+ (instancetype)sharedMusicTool
{
if (_instance == nil) { // 防止频繁加锁
@synchronized(self) {
if (_instance == nil) { // 防止创建多次
_instance = [[self alloc] init];
}
}
}
return _instance;
}
- (id)copyWithZone:(NSZone *)zone
{
return _instance;
}
@end
2.MRC