iOS 5 中的单例? [英] Singleton in iOS 5?

查看:14
本文介绍了iOS 5 中的单例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个用于单例的 iOS 以前版本的实现,如下所示:

Hi I had an implementation previous versions of iOS for a singleton as follows:

.h 文件

@interface CartSingleton : NSObject 
{

}
+(CartSingleton *) getSingleton;

.m 文件

@implementation CartSingleton

static CartSingleton *sharedSingleton = nil;

+(CartSingleton *) getSingleton
{
    if (sharedSingleton !=nil)
       {
        NSLog(@"Cart has already been created.....");
        return sharedSingleton;
       }
    @synchronized(self)
   {
    if (sharedSingleton == nil)
       {
        sharedSingleton = [[self alloc]init];
        NSLog(@"Created a new Cart");
       }
   }
    return sharedSingleton;
}
//==============================================================================
+(id)alloc
{
    @synchronized([CartSingleton class])
   {
    NSLog(@"inside alloc");
    NSAssert(sharedSingleton == nil, @"Attempted to allocate a second instance of a singleton.");
    sharedSingleton = [super alloc];
    return sharedSingleton;
   }

    return nil;
}

//==============================================================================
-(id)init
{
    self = [super init];
}

然而,我在网上看到人们使用以下代码实现了单例设计模式:

However on the web I see people have implemented the Singleton design pattern using this code:

+ (id)sharedInstance
{
  static dispatch_once_t pred = 0;
  __strong static id _sharedObject = nil;
  dispatch_once(&pred, ^{
    _sharedObject = [[self alloc] init]; // or some other init method
  });
  return _sharedObject;
}

请有经验的人指导一下.我是一个新手,在单例的旧 iOS 实现和新的 iOS 实现之间完全混淆,哪个是正确的?

Could someone who is experience please guide me. Im a newbie and thoroughly confused between the old iOS implementation of the Singleton and the new one and which is the correct one?

非常感谢

推荐答案

严格来说,你必须使用:

Strictly speaking, you must use:

+ (MySingleton*) instance {
     static dispatch_once_t _singletonPredicate;
     static MySingleton *_singleton = nil;

     dispatch_once(&_singletonPredicate, ^{
        _singleton = [[super allocWithZone:nil] init];
     });

     return _singleton;
 }

 + (id) allocWithZone:(NSZone *)zone {
      return [self instance];
 }

现在你保证不能调用 alloc/init 并创建另一个实例.

Now you guarantee that one cannot call alloc/init and create another instance.

说明:实例方法在类级别,是您获取单例引用的主要访问方法.该方法仅使用 dispatch_once() 内置队列,该队列只会执行一次块.运行时如何保证块只执行一次?使用您提供的谓词(dispatch_once_t 类型).这种低级调用将保证即使有多个线程试图调用它,也只有一个成功,其他的等到第一个完成后再返回.

Explanation: The instance method is at the class level and is your main access method to get a reference to the singleton. The method simply uses the dispatch_once() built-in queue that will only execute a block once. How does the runtime guarantee that the block is only executed once? Using the predicate you supply (of type dispatch_once_t). This low-level call will guarantee that even if there are multiple threads trying to call it, only one succeeds, the others wait until the first one is done and then returns.

我们重写 allocWithZone 的原因是因为 alloc 调用 allocWithZone 传递 nil 作为区域(对于默认区域).为了防止流氓代码分配和初始化另一个实例,我们重写 allocWithZone 以便传回的实例是已经初始化的单例.这可以防止创建第二个实例.

The reason we override allocWithZone is because alloc calls allocWithZone passing nil as the zone (for the default zone). To prevent rogue code from allocating and init-ializing another instance we override allocWithZone so that the instance passed back is the already initialized singleton. This prevents one from creating a second instance.

这篇关于iOS 5 中的单例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆