zoukankan      html  css  js  c++  java
  • OC单例快速实现

    首先新建一个头文件,定义如下宏:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    // .h文件的实现
    #define SingletonH(methodName) + (instancetype)shared##methodName;
     
    // .m文件的实现
    #if __has_feature(objc_arc) // 是ARC
    #define SingletonM(methodName)
    static id _instace = nil;
    + (id)allocWithZone:(struct _NSZone *)zone
    {
    if (_instace == nil) {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    _instace = [super allocWithZone:zone];
    });
    }
    return _instace;
    }
    - (id)init
    {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    _instace = [super init];
    });
    return _instace;
    }
    + (instancetype)shared##methodName
    {
    return [[self alloc] init];
    }
    + (id)copyWithZone:(struct _NSZone *)zone
    {
    return _instace;
    }
    + (id)mutableCopyWithZone:(struct _NSZone *)zone
    {
    return _instace;
    }
     
    #else // 不是ARC
     
    #define SingletonM(methodName)
    static id _instace = nil;
    + (id)allocWithZone:(struct _NSZone *)zone
    {
    if (_instace == nil) {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    _instace = [super allocWithZone:zone];
    });
    }
    return _instace;
    }
    - (id)init
    {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    _instace = [super init];
    });
    return _instace;
    }
    + (instancetype)shared##methodName
    {
    return [[self alloc] init];
    }
    - (oneway void)release
    {
    }
    - (id)retain
    {
    return self;
    }
    - (NSUInteger)retainCount
    {
    return 1;
    }
    + (id)copyWithZone:(struct _NSZone *)zone
    {
        return _instace;
    }
     
    + (id)mutableCopyWithZone:(struct _NSZone *)zone
    {
        return _instace;
    }
     
    #endif

    然后在你定义单例类的

    .h 文件 写 SingletonH(MyMethodName) 

    .m 文件写 SingletonM(MyMethodName)

    搞定!

  • 相关阅读:
    JavaScript 将数组中具有相同值的对象 取出组成新的数组
    css样式初始化
    javaScript如何跳出多重循环break、continue
    vue-cli中webpack配置解析
    Invalid prop: type check failed for prop "XXX". Expected String, got Object.
    vue-cli构建项目添加网站ico的logo
    vue-cli构建的vue项目打包后css引入的背景图路径不对的问题
    从零开始使用vue-cli搭建一个vue项目及注意事项
    安装npm及cnpm(Windows)
    面试题8:旋转数组中的最小数字
  • 原文地址:https://www.cnblogs.com/yate1996/p/4639172.html
Copyright © 2011-2022 走看看