iOS-简化单例模式(定义成宏 以后通用)

// .h文件
#define HMSingletonH + (instancetype)sharedInstance;

// .m文件
#define HMSingletonM 
static id _instance; 
 
+ (id)allocWithZone:(struct _NSZone *)zone 
{ 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
        _instance = [super allocWithZone:zone]; 
    }); 
    return _instance; 
} 
 
+ (instancetype)sharedInstance 
{ 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
        _instance = [[self alloc] init]; 
    }); 
    return _instance; 
} 
 
- (id)copyWithZone:(NSZone *)zone 
{ 
    return _instance; 
}
原文地址:https://www.cnblogs.com/zhuyaguang/p/4853400.html