美文网首页
iOS 单例宏定义记录

iOS 单例宏定义记录

作者: Her0cheN | 来源:发表于2020-03-23 23:20 被阅读0次

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

// 使用方法:.h 添加
// MYSingletonH(SingletonClass)//单例模式
// 使用方法:.m 添加
// MYSingletonM(SingletonClass)//单例模式


// MYSingletonH.h文件 - 创建MYSingletonH.h全局文件
#define MYSingletonH(name) + (instancetype)shared##name;

#if __has_feature(objc_arc)

    #define MYSingletonM(name) \
    static id _instace; \
 \
    + (id)allocWithZone:(struct _NSZone *)zone \
    { \
        if (_instace == nil) {\
            _instace = [super allocWithZone:zone]; \
            NSLog(@"单例创建!--- 上架删除该行"); \
        } \
        return _instace; \
    } \
 \
    + (instancetype)shared##name \
    { \
        if (_instace == nil) {\
            _instace = [[self alloc]init]; \
        } \
      return _instace; \
  \
    } \
 \
    - (id)copyWithZone:(NSZone *)zone \
    { \
        return _instace; \
    }

#else

    #define MYSingletonM(name) \
    static id _instace; \
 \
    + (id)allocWithZone:(struct _NSZone *)zone \
    { \
        static dispatch_once_t onceToken; \
        dispatch_once(&onceToken, ^{ \
            _instace = [super allocWithZone:zone]; \
        }); \
        return _instace; \
    } \
 \
    + (instancetype)shared##name \
    { \
        static dispatch_once_t onceToken; \
        dispatch_once(&onceToken, ^{ \
            _instace = [[self alloc] init]; \
        }); \
        return _instace; \
    } \
 \
    - (id)copyWithZone:(NSZone *)zone \
    { \
        return _instace; \
    } \
 \
    - (oneway void)release { } \
    - (id)retain { return self; } \
    - (NSUInteger)retainCount { return 1;} \
    - (id)autorelease { return self;}

#endif

相关文章

  • iOS 单例宏定义记录

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

  • iOS单例--宏定义

    如下是单例,是一个宏。这样只有工程中用到单例直接用此宏创建,大大节约时间。 如下是通过宏文件如何创建单例。在.h中...

  • iOS 单例

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

  • ios宏定义单例模式

  • 0922 宏定义通杀单例

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

  • 宏定义单例

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

  • ios 单例宏

    单例的初始化在整个app生命周期内(非对象的生命周期)只执行一次,本文介绍通过宏来实现单例方法的定义。代码如下:-...

  • iOS单例宏

    开发中我们无可避免的使用到单例,单例的具体作用不多说了,这里记录一下单例宏的写法。新建一个header file,...

  • iOS 单例宏

    #if __has_feature(objc_arc) #define SYNTHESIZE_SINGLETON_...

  • 单例

    单例 单例宏

网友评论

      本文标题:iOS 单例宏定义记录

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