码迷,mamicode.com
首页 > 其他好文 > 详细

单例宏抽取

时间:2015-11-01 21:21:55      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:


#define SingleInterface(name) +(instancetype)share##name


#if __has_feature(objc_arc)

// ARC

#define SingleImplementation(name)  +(instancetype)share##name \

{ \

    return [[self alloc] init]; \

} \

+ (instancetype)allocWithZone:(struct _NSZone *)zone \

{ \

    static id instance; \

    static dispatch_once_t onceToken; \

    dispatch_once(&onceToken, ^{ \

        instance = [super allocWithZone:zone]; \

    }); \

    return instance; \

} \

- (id)copyWithZone:(NSZone *)zone \

{ \

    return self; \

} \

- (id)mutableCopyWithZone:(NSZone *)zone \

{ \

    return self; \

}

#else

// MRC

#define SingleImplementation(name)  +(instancetype)share##name \

{ \

    return [[self alloc] init]; \

} \

+ (instancetype)allocWithZone:(struct _NSZone *)zone \

{ \

    static id instance; \

    static dispatch_once_t onceToken; \

    dispatch_once(&onceToken, ^{ \

        instance = [super allocWithZone:zone]; \

    }); \

    return instance; \

} \

- (id)copyWithZone:(NSZone *)zone \

{ \

    return self; \

} \

- (id)mutableCopyWithZone:(NSZone *)zone \

{ \

    return self; \

} \

- (oneway void)release \

{} \

- (instancetype)retain \

{ \

    return self; \

} \

- (NSUInteger)retainCount \

{ \

    return MAXFLOAT; \

}

#endif

单例宏抽取

标签:

原文地址:http://my.oschina.net/codebywj/blog/524640

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!