您的位置:首页 > 移动开发 > IOS开发

iOS开发笔记--一些实用的宏定义

2014-06-19 15:40 495 查看

这是在工作中常用的一些比较实用的宏定义

// 声明单件类的类方法
#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; \
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: