• 单例的使用总结


    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
  • 相关阅读:
    Luogu P1396 营救
    Luogu P1339 热浪Heat Wave
    哈夫曼树学习笔记
    题解 CF1372C
    题解 CF 1372 B
    题解 CF 1372A
    题解 UVA1193 Radar Installation
    题解 洛谷 P2287 [USACO07NOV]Sunscreen G
    洛谷 P1080 国王游戏 题解
    牛客练习赛 66C公因子 题解
  • 原文地址:https://www.cnblogs.com/easonoutlook/p/2642805.html
Copyright © 2020-2023  润新知