美文网首页
真正的单例就应该这样写

真正的单例就应该这样写

作者: 牛1688 | 来源:发表于2017-09-11 10:13 被阅读0次

@implementationfirstVc

//管方说这是由于历史原因还存在.参数管方建义传入null

+(instancetype)allocWithZone:(struct_NSZone*)zone{

return[superallocWithZone:zone];

}

+(instancetype)firstVC{

staticfirstVc* vc;

staticdispatch_once_toneToken;

dispatch_once(&oneToken, ^{

//vc = [[superalloc]init];

vc = [[firstVcallocWithZone:nil]init];

});

returnvc;

}

//重写alloc

+(instancetype)alloc{

return[firstVcfirstVC];

}

-(instancetype)init{

self= [superinit];

returnself;

}

//第二种写法

@implementationfirstVc

staticfirstVc*shareVc;

//管方说这是由于历史原因还存在.参数管方建义传入null

+(instancetype)allocWithZone:(struct_NSZone*)zone{

return[superallocWithZone:zone];

}

+(instancetype)firstVC{

//

//static firstVc * vc;

//static dispatch_once_t oneToken;

//

//dispatch_once(&oneToken, ^{

//vc = [[super alloc] init];

//});

//return vc;

if(!shareVc) {

shareVc= [[superalloc]init];

}

returnshareVc;

}

//重写alloc

+(instancetype)alloc{

return[firstVcfirstVC];

}

-(instancetype)init{

self= [superinit];

returnself;

}

相关文章

  • 真正的单例就应该这样写

    @implementationfirstVc //管方说这是由于历史原因还存在.参数管方建义传入null +(in...

  • iOS单例模式的正确写法

    单例模式很常见,但是,能真正把单利模式写对的却很少。在iOS中,一般我们都是用官方推荐的写法来写单例: URLMa...

  • OC和Swift单例的写法

    一 、OC中单例的写法:1.普通单例的写法 2. 利用多线程来写单例 #pragma mark --- 普通单例写...

  • iOS知识点总结(1)

    1. iOS单例的实现方式? 之前总是这样写: static Singleton *shareSingleton ...

  • Java编程——单例模式的安全性

    单例模式,我想大家再熟悉不过了,不过本文不是介绍单例模式该怎么写的。 本文来说说怎么破坏一个单例,让你写的单例变成...

  • iOS单例安全写法

    单例模式很常见,但是,能真正把单利模式写对的却很少。在iOS中,一般我们都是用官方推荐的写法来写单例: 这也是我们...

  • dispatch_once创建单例

    写蓝牙的时候,需要使用到单例,于是参照网上的例子,使用写了这样的一个单例,权当记录: dispatch_once被...

  • Swift Singleton Theory

    在OC中我们经常这样写单例: @implemention MyManager+ (id)sharedManager...

  • 单例模式

    单例模式的特点 单例类只能有一个实例 单例类必须自己创建自己的唯一实例 单例类必须给所有其它对象提供这一实例 写单...

  • 单例模式 Singleton Pattern

    单例模式-菜鸟教程 iOS中的设计模式——单例(Singleton) iOS-单例模式写一次就够了 如何正确地写出...

网友评论

      本文标题:真正的单例就应该这样写

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