zoukankan      html  css  js  c++  java
  • 单例的使用总结

    static RootViewController *sharedRootController = nil;

     
    +(RootViewController *) sharedController{
        @synchronized(self){
            if (sharedRootController == nil) {
                sharedRootController = [[[self alloc] init] autorelease];
            }
        }
        return singleController;
    }
    +(id) allocWithZone:(NSZone *)zone{
        @synchronized(self){
            if (sharedRootController == nil) {
                sharedRootController = [super allocWithZone:zone];
                return sharedRootController;
            }
        }
        return nil;
    }
     
     
    1. synchronized   这个主要是考虑多线程的程序,这个指令可以将{ } 内的代码限制在一个线程执行,如果某个线程没有执行完,其他的线程如果需要执行就得等着。
    2、 allocWithZone 这个是重载的,因为这个是从制定的内存区域读取信息创建实例,所以如果需要的单例已经有了,就需要禁止修改当前单例。所以返回 nil
    3、 关于autorelease ,   iOS 上的程序,对于创建用于函数返回值的,都应该考虑 autorelease
  • 相关阅读:
    控件右键菜单的实现以及选中后勾选
    DataGridView控件使用
    return,continue,break的区别
    break和continue的区别 循环终止办法
    事件
    跨线程改变控件属性 线程调用带参数方法
    XML配置文件相关
    抽象类及与接口的区别
    字典
    Oracle学习第一天
  • 原文地址:https://www.cnblogs.com/easonoutlook/p/2642805.html
Copyright © 2011-2022 走看看