美文网首页
iOS官方推荐的单例写法

iOS官方推荐的单例写法

作者: VincentHK | 来源:发表于2017-03-26 14:35 被阅读0次

#define HMSingleton_h(name)  + (instancetype)shared##name;

#if __has_feature(objc_arc) // 是arc环境

#define HMSingleton_m(name)  + (instancetype)shared##name {\

return [[self alloc] init];\

}\

\

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

static dispatch_once_t onceToken;\

static id instance = nil;\

dispatch_once(&onceToken, ^{ \

instance = [super allocWithZone:zone];\

});\

return instance;\

}\

\

- (id)copyWithZone:(nullable NSZone *)zone {\

return self;\

}

#else // MRC环境

#define HMSingleton_m(name)  + (instancetype)shared##name {\

return [[self alloc] init];\

}\

\

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

static dispatch_once_t onceToken;\

static id instance = nil;\

dispatch_once(&onceToken, ^{ \

instance = [super allocWithZone:zone];\

});\

return instance;\

}\

\

- (id)copyWithZone:(nullable NSZone *)zone {\

return self;\

}\

- (oneway void)release {\

\

}\

\

- (instancetype)autorelease {\

return self;\

}\

\

- (instancetype)retain {\

return self;\

}\

\

- (NSUInteger)retainCount {\

return 1;\

}

#endif

相关文章

  • ios~单例模式:

    在iOS OC中,一般我们都是用官方推荐的写法来写单例:GCD方式单例 分析单例 static SharedPer...

  • 单例的2种写法

    单例模式是iOS开发中最常用的设计模式,iOS的单例模式有两种官方写法,如下: 1,常用写法 import "Se...

  • iOS官方推荐的单例写法

    #define Singleton_h(name) + (instancetype)shared##name; #...

  • iOS官方推荐的单例写法

    #define HMSingleton_h(name) + (instancetype)shared##name;...

  • Objective-c实现单例模式

    首先放一段苹果官方推荐的单例模式写法,兼容iOS 4以下的系统 Creating a Singleton Inst...

  • iOS单例模式的正确写法

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

  • iOS单例安全写法

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

  • iOS-两种单例模式的实现

    单例模式是开发中最常用的写法之一,创建一个单例很多办法,iOS的单例模式有两种官方写法,如下: 不使用GCD 某些...

  • iOS 单例模式

    关于单例模式的详解,看完这几篇,就完全了然了。iOS 单例模式iOS中的单例模式iOS单例的写法

  • iOS开发之进阶篇(5)—— 单例

    目录 最终推荐写法 何为单例? 对象的创建 单例写法的讨论过程 1. 最终推荐写法 1.1 OC SingleOb...

网友评论

      本文标题:iOS官方推荐的单例写法

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