• ARC和MRC 兼容的单例模式


    一、ARC下的单例实现

      说明:在用户实例化的方法控制单次执行,同时开放单例的初始化方法。

    -(instancetype)init{
    
        self=[super init];
        if(self){
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
            });
        }
        return self;
        
    
    }
    
    static id instance; 
    +(instancetype)allocWithZone:(struct _NSZone *)zone{ 
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{ 
        instance=[super allocWithZone:zone]; 
        }); 
    return instance;
    } 
     
     
    + (instancetype) shareAudio{
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{ 
        instance=[[self alloc]init]; 
        }); 
        return instance; 
    } 
     
    +(id)copyWithZone:(struct _NSZone *)zone{ 
        return instance;
    }
    二、MRC下的单例实现

       说明:在用户实例化的方法控制单次执行,同时开放单例的初始化方法,由于当前为MRC所以需要控制参内存管理的方法单次执行,因此相比ARC需要增加:

    static id instance; 
    +(instancetype)allocWithZone:(struct _NSZone *)zone{ 
        static dispatch_once_t onceToken; 
        dispatch_once(&onceToken, ^{ 
            instance=[super allocWithZone:zone]; 
        }); 
        return instance; 
    } 
     
     
    + (instancetype) shareAudio{
        static dispatch_once_t onceToken; 
        dispatch_once(&onceToken, ^{ 
            instance=[[self alloc]init]; 
        }); 
        return instance; 
    } 
    -(oneway void)release{ 
    } 
     
    -(instancetype)autorelease{ 
        return instance; 
    } 
     
    -(instancetype)retain{ 
        return instance; 
    } 
     
    -(NSUInteger)retainCount{ 
        return 1; 
    }
    三、兼容MRC 和ARC的宏定义

       说明:为了方便后期的引用,可以将单例抽取为宏定义,鉴于抽取的时候考虑到当前的手动计数和自动计数因此引入条件编译:

    #if !__has_feature(objc_arc)
    ======当前是ARC
    #else
    ======当前是MRC
    #endif
    
    代码:
    #define singleton_h(name)  + (instancetype) share##name;
    
    
    #if !__has_feature(objc_arc)
    #define singleton_m(name)
     static id instance;
    +(instancetype)allocWithZone:(struct _NSZone *)zone{
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            instance=[super allocWithZone:zone];
        });
        return instance;
    }
    
    
    + (instancetype) share##name{
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            instance=[[self alloc]init];
        });
        return instance;
    }
    -(oneway void)release{
    }
    
    -(instancetype)autorelease{
        return instance;
    }
    
    -(instancetype)retain{
        return instance;
    }
    
    +(id)copyWithZone:(struct _NSZone *)zone{
        return instance;
    }
    -(NSUInteger)retainCount{
        return 1;
    }
    
    #else
    
    #define singleton_m(name)
    static id instance;
    +(instancetype)allocWithZone:(struct _NSZone *)zone{
      static dispatch_once_t onceToken;
      dispatch_once(&onceToken, ^{
      instance=[super allocWithZone:zone];
    });
      return instance;
    }
    
    
    + (instancetype) share##name{
      static dispatch_once_t onceToken;
      dispatch_once(&onceToken, ^{
       instance=[[self alloc]init];
      });
      return instance;
    }
    
    +(id)copyWithZone:(struct _NSZone *)zone{
       return instance;
    }
    #endif
    四、文件引用

    1 在.h文件引用singleton_h(audio);

    2 在.m文件引用singleton_m(audio);

    作者:杰瑞教育
    出处:http://www.cnblogs.com/jerehedu/ 
    本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
     
  • 相关阅读:
    「题解」300iq Contest 2 B Bitwise Xor
    「题解」agc031_e Snuke the Phantom Thief
    「题解」agc031_c Differ by 1 Bit
    「题解」NWRRC2017 Joker
    「题解」NWRRC2017 Grand Test
    「题解」USACO15FEB Fencing the Herd G
    zsh: command not found:xxx 解决方法
    curl
    redis搭建
    http 缓存分为客户端缓存/服务端缓存/数据库缓存
  • 原文地址:https://www.cnblogs.com/jerehedu/p/4359918.html
Copyright © 2020-2023  润新知