单例模式

来源:互联网 发布:psdto3d软件免费下载 编辑:程序博客网 时间:2024/04/29 12:20

一、方法一:通过一次执行代码实现

//全局变量static  id sharedInstance = nil;+ (instancetype)sharedInstance{//一次执行代码    static dispatch_once_t oncePredicate;    dispatch_once(&oncePredicate, ^{        sharedInstance = [[self alloc] init];    });    return sharedInstance;}


二、方法二:常规实现方法

//全局变量static id shareInstance = nil;+(instancetype)sharedInstance{    //只初始化一次    if (shareInstance == nil) {        shareInstance = [[self alloc] init];    }    return shareInstance;}




0 0