首页 > 其他 > 详细

单例宏抽取

时间:2015-11-01 21:20:47      阅读:264      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!