宏定义抽取单例

// cmd + n 新建Header File 文件名: Singleton.h

// .h
#define singleton_interface(className) + (instancetype)shared##className;

// .m
// 注意:最后一句不要斜线
#define singleton_implementation(className) static className *_instace; + (id)allocWithZone:(struct _NSZone *)zone { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instace = [super allocWithZone:zone]; }); return _instace; } + (instancetype)shared##className { if (_instace == nil) { _instace = [[className alloc] init]; } return _instace; }
原文地址:https://www.cnblogs.com/orzmj123/p/3512208.html