美文网首页程序员
单例的宏定义

单例的宏定义

作者: Uncle丶shuai | 来源:发表于2017-10-25 17:30 被阅读496次

用宏定义把单例忘了吧。。

#ifndef Singleton_h

#define Singleton_h

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

#define SingletonImplementation(name) \

static id _instance; \

+ (instancetype)shared##name \

{ \

static dispatch_once_t onceToken; \

dispatch_once(&onceToken, ^{ \

_instance = [[self alloc] init]; \

}); \

return _instance; \

} \

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

{ \

static dispatch_once_t onceToken; \

dispatch_once(&onceToken, ^{ \

_instance = [super allocWithZone:zone]; \

}); \

return _instance; \

} \

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

{ \

return _instance; \

}

#endif/* Singleton_h */

相关文章

  • iOS 单例

    单例模式实现不能使用继承 定义单例实现 简写 定义单例实现宏

  • 0922 宏定义通杀单例

    1、单例宏定义源码 说明此宏定义精华就是把声明文件和执行文件都放在宏定义了,而且针对不同的类,生成不同的单例,使用...

  • iOS 单例宏定义记录

    MYSingleton.h : 单例宏定义 - 头文件

  • 宏定义单例

    新建.h文件## 在.h文件中代码如下: 使用方法: 新建类FirstFirst.h中 First.m中 Firs...

  • 单例的宏定义

    用宏定义把单例忘了吧。。 #ifndef Singleton_h#define Singleton_h#defin...

  • 单例的宏定义

    序言 单例的使用在我们开发iOS程序的时候的使用率是非常高的,在我们写一个单例的时候,可能不止会用到一个单例,然而...

  • 单例的宏定义

    #define DEFINE_SINGLETON_FOR_HEADER(className) \ \ + (cla...

  • 单例的宏定义

    在开发过程中不难避免会用到单例.但是每个类都要去单独写单例的定义方式 感觉有点麻烦!下面就用宏定义一个单列来简化下...

  • 单例

    单例 单例宏

  • 宏定义单例类

网友评论

    本文标题:单例的宏定义

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