zoukankan      html  css  js  c++  java
  • IOS 单例 创建方式

    @implementation Me

    static Car *sharedInstance= nil;//声明一个静态对象引用并赋为nil

     

    +(Me *) sharedInstance//声明类方法(+为类方法,也就是Java中的静态方法)

    {

         if(!sharedInstance)

         {

              sharedInstance = [[self alloc] init];

         }

         return sharedInstance;

    }

    @end

     

    //覆盖allocWithZone:方法可以防止任何类创建第二个实例。使用synchronized()可以防止多个线程同时执行该段代码(线程锁)

     

    +(id)allocWithZone:(NSZone *) zone

    {

         @synchronized(self)

         {

              if(sharedInstance == nil)

              {

                   sharedInstance = [super allocWithZone:zone];

                   return sharedInstance;

              }

         }

         return sharedInstance;

    }

     直接类名调用类方法就行了

  • 相关阅读:
    全局配置策略
    RESTful api介绍
    AJAX
    django cookie session 自定义分页
    mysql 索引优化
    yii2 response响应配置
    Django中的信号
    django orm相关操作
    django orm介绍以及字段和参数
    django form和ModelForm组件
  • 原文地址:https://www.cnblogs.com/mohe/p/3443486.html
Copyright © 2011-2022 走看看