ios 单列模式

来源:互联网 发布:美国动画 知乎 编辑:程序博客网 时间:2024/06/04 19:22

今天发现原来有两种可实现单例模式的方法,现在贴出来与大家分享

第一种:这个是以前一直在使用的。

static Control *mControl;...+(id)getShare{    if (mControl == nil) {        mControl = [[Control alloc] init];    }    return mControl;}

第二种:这是今天新发现的。

+ (BDMultiDownloader *)shared{    static dispatch_once_t once;    static BDMultiDownloader * singleton;    dispatch_once(&once, ^ { singleton = [[BDMultiDownloader alloc] init]; });    return singleton;}

dispatch_once_t这是系统函数

0 0
原创粉丝点击