zoukankan      html  css  js  c++  java
  • OC基础 单例

    #undef  AS_SINGLETON  
        #define AS_SINGLETON( __class )   
                + (__class *)sharedInstance;  
              
            #undef  DEF_SINGLETON  
            #define DEF_SINGLETON( __class )   
                    + (__class *)sharedInstance   
                    {   
                        static dispatch_once_t once;   
                        static __class * __singleton__;   
                       dispatch_once( &once, ^{ __singleton__ = [[__class alloc] init]; } );   
                        return __singleton__;   
                }  

    上面是用宏来定义单例。

    下面介绍我们常用的两种单例的写法:

    第一种

    static SingleClass *single = nil;
    + (id)shareInstance {
        if(!single)
        {
            single = [[SingleClass alloc] init];
        }
        return single;
    }

    第二种

    + (id)shareInstance
    {
        static SingleClass *single = nil;
        static dispatch_once_t predicate;
        dispatch_once(&predicate, ^{
            single = [[self alloc] init];
        });
        return single;
    }
  • 相关阅读:
    .net core使用EasyNetQ做EventBus
    .NET Core DI 手动获取注入对象
    设置Jexus开机启动
    centos7开放关闭防火墙端口
    linux防火墙开放端口
    转 Jexus-5.6.3使用详解
    Mysql存储过程
    Reids笔记
    Lucene笔记
    Spring Junit 测试用例
  • 原文地址:https://www.cnblogs.com/DWdan/p/4875086.html
Copyright © 2011-2022 走看看