美文网首页
两种方式创建单例

两种方式创建单例

作者: 一直努力奋斗的女程序员 | 来源:发表于2016-06-23 12:59 被阅读0次

a.GCD方式:

static id _instance;

+ (instancetype)allocWithZone:(struct _NSZone *)zone

{

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

_instance = [super allocWithZone:zone];

});

return _instance;

}

b.互斥锁方式:

static id _instance;

+ (instancetype)allocWithZone:(struct _NSZone *)zone

{

@synchronized(self) {

if (_instance == nil) {

_instance = [super allocWithZone:zone];

}

}

return _instance;

}

相关文章

  • iOS 单例模式 - 单例对象销毁【@synchronized】

    单例对象的创建方式 单例.h 文件的实现 单例.m 文件的实现 单例对象的销毁【@synchronized创建方式...

  • iOS 单例模式 - 单例对象销毁【GCD】

    单例对象的创建方式 单例.h 文件的实现 单例的.m 文件的实现 单例对象的销毁【GCD创建的方式】 使用单例对象...

  • 8. 单例

    单例 1、GCD方式创建单例 2、互斥锁方式 销毁

  • 两种方式创建单例

    a.GCD方式: staticid_instance; +(instancetype)allocWithZone:...

  • Swift 单例传值

    回顾 Objc 创建单例的方式: 1.创建单例 2.给单例的属性赋值 3.输出单例的属性值

  • iOS单例类的创建和简单使用

    单例类的两种创建方式: .h文件 .m文件 在其他类中使用单例类中的属性(比如字符串:@“900”)。先在该类中导...

  • iOS单例两种创建方式

    1、GCD方式创建单例 staticid_instance; +(instancetype)allocWithZo...

  • 懒加载和单例

    懒加载 声明属性 重写get方法 Swift 单例的创建方式 方式一:创建单例工厂方法(重写alloc完善) 声明...

  • 单例模式下的线程安全

    首先我们回顾一下,单例模式的概念:单例模式:自我创建对象,唯一实例。关键点:构造函数是私有的。 主要分为两种方式:...

  • ios 确保单例对象的唯一性

    之前创建单例是使用下面的方式: 这种方式创建的单例,如果外部使用了alloc方法来创建实例,或者copy方法拷贝了...

网友评论

      本文标题:两种方式创建单例

      本文链接:https://www.haomeiwen.com/subject/vonndttx.html