zoukankan      html  css  js  c++  java
  • 使用宏定义创建单例

    //单例
    
    #ifndef KCSingleton_h
    #define KCSingleton_h
    
    #pragma mark 接口.h中的定义
    //由于宏定义里有需要替换的内容所以定义一个变量className
    //##用于分割、连接字符串
    #define singleton_interface(className) +(className *)shared##className;
    
    #pragma mark 实现.m
    //在代码中用于连接宏定义,以实现多行定义
    #define singleton_implementation(className) 
    static className *_instance;
    +(id)shared##className{
        if(!_instance){
            _instance=[[self alloc]init];
        }
        return _instance;
    }
    +(id)allocWithZone:(struct _NSZone *)zone{
        static dispatch_once_t dispatchOnce;
        dispatch_once(&dispatchOnce, ^{
            _instance=[super allocWithZone:zone];
        });
        return _instance;
    }
    
    #endif
  • 相关阅读:
    P3332 [ZJOI2013]K大数查询
    树上最短路---------------树链剖分,优化建边。
    BZOJ_4386
    2016_1_13(3)
    2016_1_13(2)
    2016_1_13
    BZOJ_1698
    BZOJ_4152
    BZOJ_3110
    BZOJ_2141
  • 原文地址:https://www.cnblogs.com/hxwj/p/4418819.html
Copyright © 2011-2022 走看看