- // 声明单件类的类方法
- #define DECLARE_SINGLETON_FOR_CLASS(classname) \
- + (classname*)shared##classname;
- // 获取单件实例的宏定义
- #define GET_SINGLETON_FOR_CLASS(classname) \
- [classname shared##classname]
- // 合成单件类
- #define SYNTHESIZE_SINGLETON_FOR_CLASS(classname) \
- \
- static classname *shared##classname = nil; \
- \
- + (classname *)shared##classname \
- { \
- @synchronized(self) \
- { \
- if (shared##classname == nil) \
- { \
- shared##classname = [[self alloc] init]; \
- } \
- } \
- \
- return shared##classname; \
- } \
- \
- + (id)allocWithZone:(NSZone *)zone \
- { \
- @synchronized(self) \
- { \
- if (shared##classname == nil) \
- { \
- shared##classname = [super allocWithZone:zone]; \
- return shared##classname; \
- } \
- } \
- \
- return nil; \
- } \
- \
- - (id)copyWithZone:(NSZone *)zone \
- { \
- return self; \
- } \
- \
- - (id)retain \
- { \
- return self; \
- } \
- \
- - (NSUInteger)retainCount \
- { \
- return NSUIntegerMax; \
- } \
- \
- - (oneway void)release \
- { \
- } \
- \
- - (id)autorelease \
- { \
- return self; \
- }
iOS单例的宏定义
最新推荐文章于 2017-11-07 16:57:41 发布