zoukankan      html  css  js  c++  java
  • 单例模式ARC和非ARC

    ARC环境下的单例模式:

    static id _instance = nil; 
    + (id)allocWithZone:(struct _NSZone *)zone 
    { 
        if (_instance == nil) { 
             
            static dispatch_once_t onceToken; 
             
            dispatch_once(&onceToken, ^{ 
                _instance = [super allocWithZone:zone]; 
            }); 
             
        } 
         
        return _instance; 
         
    } 
     
    - (id)init 
    { 
         
        static dispatch_once_t onceToken; 
        dispatch_once(&onceToken, ^{ 
            _instance = [super init]; 
        }); 
        return _instance; 
    } 
     
    + (instancetype)sharedMethodName
    { 
        return [[self alloc] init]; 
    }

    非ARC模式下的单例模式:

    static id _instance = nil; 
    + (id)allocWithZone:(struct _NSZone *)zone 
    { 
        if (_instance == nil) { 
             
            static dispatch_once_t onceToken; 
             
            dispatch_once(&onceToken, ^{ 
                _instance = [super allocWithZone:zone]; 
            }); 
             
        } 
         
        return _instance; 
         
    } 
     
    - (id)init 
    { 
         
        static dispatch_once_t onceToken; 
        dispatch_once(&onceToken, ^{ 
            _instance = [super init]; 
        }); 
        return _instance; 
    } 
     
    + (instancetype)sharedMethodName
    { 
        return [[self alloc] init]; 
    } 
     
    - (oneway void)release 
    { 
         
    } 
     
    - (id)retain 
    { 
        return self; 
    } 
     
    - (NSUInteger)retainCount 
    { 
        return 1; 
    }

    如何判断当前的环境是arc还是非arc  可以这样写:

    #if __has_feature(objc_arc)  //是arc

    //这里写arc下的代码

    #else           //非arc

    //这里写非acr下的代码

    #endif

  • 相关阅读:
    HTTP的三次握手
    HTTP协议的发展历史
    二分图 (最大匹配 + 最小点覆盖 + 最少路径覆盖 + 最大独立集)
    昂贵的聘礼 POJ
    Cow Contest POJ 3660 (Floyed ) (最短路专题)
    B-number HDU
    You Are the One HDU
    0 or 1 HDU
    Trie树模板 + 例题
    The Shortest Path in Nya Graph HDU
  • 原文地址:https://www.cnblogs.com/syios/p/4797783.html
Copyright © 2011-2022 走看看