• IOS单例的设计模式


    static SurveyRunTimeData *sharedObj = nil; //第一步:静态实例,并初始化置为nil。

    @implementation SurveyRunTimeData

    + (SurveyRunTimeData*) sharedInstance    //第二步:实例构造检查静态实例是否为nil。

    {    

    @synchronized (self)     {

            if (sharedObj == nil)

            {            

          [[self alloc] init];

            }    

    }

        return sharedObj;

    }

    + (id) allocWithZone:(NSZone *)zone         //第三步:重写allocWithZone方法 {

        @synchronized (self) {        

      if (sharedObj == nil) {

                sharedObj = [super allocWithZone:zone];

                return sharedObj;

            }

        }

        return nil;

    }

    - (id) copyWithZone:(NSZone *)zone 

            //第四步

    {    

    return self;

    }

    - (id) retain {

        return self;

    }

    - (unsigned) retainCount {

        return UINT_MAX;

    }

    - (oneway void) release {   

      }

    - (id) autorelease {

        return self;

    }

    - (id)init {

        @synchronized(self) {

            [super init];            

    //往往放一些要初始化的变量.        

    return self;    

    }

    }

    @end

  • 相关阅读:
    pip 8 安装
    zabbix server配置文件
    双代号网络图、双代号时标网络图
    logrotate
    tsql 执行存储过程
    dos 加用户
    Visual Studio (VS IDE) 你必须知道的功能和技巧
    格式化数字字符串 与C#变量
    .NET中的字符串你了解多少?
    新手如何有效地学习.NET
  • 原文地址:https://www.cnblogs.com/zhibin/p/4118120.html
Copyright © 2020-2023  润新知