码迷,mamicode.com
首页 > 移动开发 > 详细

iOS单例 宏定义

时间:2015-04-09 00:47:47      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

#define singleton_interface(className) + (className *)shared##className;


// @implementation
#define singleton_implementation(className) static className *_instance; + (id)allocWithZone:(NSZone *)zone { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [super allocWithZone:zone]; }); return _instance; } + (className *)shared##className { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [[self alloc] init]; }); return _instance; }

 

用法:

@interface AccountTool : NSObject
singleton_interface(AccountTool)
@end

 

@implementation AccountTool
singleton_implementation(AccountTool)
@end

 

iOS单例 宏定义

标签:

原文地址:http://www.cnblogs.com/damiao/p/4405077.html

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