首页 > 其他 > 详细

使用宏定义创建单例

时间:2015-04-12 01:20:14      阅读:249      评论:0      收藏:0      [点我收藏+]
//单例

#ifndef KCSingleton_h
#define KCSingleton_h

#pragma mark 接口.h中的定义
//由于宏定义里有需要替换的内容所以定义一个变量className
//##用于分割、连接字符串
#define singleton_interface(className) +(className *)shared##className;

#pragma mark 实现.m
//\在代码中用于连接宏定义,以实现多行定义
#define singleton_implementation(className) static className *_instance;+(id)shared##className{    if(!_instance){        _instance=[[self alloc]init];    }    return _instance;}+(id)allocWithZone:(struct _NSZone *)zone{    static dispatch_once_t dispatchOnce;    dispatch_once(&dispatchOnce, ^{        _instance=[super allocWithZone:zone];    });    return _instance;}

#endif

 

使用宏定义创建单例

原文:http://www.cnblogs.com/hxwj/p/4418819.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!