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)

    搞定!

  • 相关阅读:
    MySQL数据库的登陆
    Mysql 数据库的介绍
    前台后台数据的传递
    header函数
    循环结构
    流程控制
    JS与PHP数组操作的不同
    HDU 3265/POJ 3832 Posters(扫描线+线段树)(2009 Asia Ningbo Regional)
    HDU 3264/POJ 3831 Open-air shopping malls(计算几何+二分)(2009 Asia Ningbo Regional)
    HDU 3262/POJ 3829 Seat taking up is tough(模拟+搜索)(2009 Asia Ningbo Regional)
  • 原文地址:https://www.cnblogs.com/yate1996/p/4639172.html
Copyright © 2011-2022 走看看