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

相关文章:

  • 2022-12-23
  • 2021-10-04
  • 2021-11-30
  • 2021-09-11
  • 2021-08-02
  • 2021-06-04
  • 2021-12-17
  • 2021-12-13
猜你喜欢
  • 2022-01-26
  • 2022-12-23
  • 2021-08-28
  • 2022-03-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案