宏定义抽取单例

// 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; \ }
posted @ 2014-01-09 14:50  yuan555  阅读(184)  评论(0编辑  收藏  举报