• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

使用 allocWithZone 返回静态

用户头像
it1352
帮助1

问题说明

根据 Big Nerd Ranch 的 iOS 编程指南中的一个想法,我尝试以下列方式定义单例对象:

Working off an idea from the Big Nerd Ranch guide to iOS programming, I'm trying to define singleton object in the following manner:

@implementation ImageStore

static ImageStore *defaultImageStore = nil;

- (id)init
{
    if (defaultImageStore) {
        return defaultImageStore;
    }

    self = [super init];
    if (self) {
        dictionary = [[NSMutableDictionary alloc] init];
    }

    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self selector:@selector(clearCache) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];

    return self;
}

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

  (ImageStore *)defaultImageStore
{
    if (!defaultImageStore) {
        defaultImageStore = [[super allocWithZone:NULL] init];
    }
    return defaultImageStore;
}

这工作正常,但分析器抱怨 allocWithZone,说

This works fine, but the analyzer complains about allocWithZone, saying

保留计数为 0 的对象返回给调用者,其中预期为 1(拥有)保留计数

Object with 0 retain counts returned to caller where a 1 (owning) retain count is expected

我认为代码只是在欺骗分析器,并且正在发生的事情是可以的,但是有没有更好的模型来做我想要做的事情?

I think the code is just tricking the analyzer and that what's happening is OK, but is there a better model to do what I'm trying to do?

正确答案

#1

我不关心所有这些事情.这是我的模式:

I don't bother with all that stuff. This is my pattern:

@implementation MyClass

@implementation MyClass

-(id) init { ... } /* normal init */

 (MyClass*) defaultMyClass
{
    static MyClass* theObject = nil;
    @synchronized([MyClass class])  // remove synchronzed block in single threaded app
    {
        if (theObject == nil)
        {
            theObject = [[self alloc] init];
        }
    }
    return theObject;
}

可能你可以在类扩展中隐藏初始化器,以便记录你不应该使用它.

Possbily you can hide the initialiser in a class extension so that it is documented that you shouldn't use it.

还有 GCD 方法(以下是从 Rob Napier 的链接中窃取的),它实际上更轻量级.

There's also the GCD method (the below is stolen from Rob Napier's link) which is actually more lightweight.

  (id) defaultMyClass
{
    static dispatch_once_t pred;
    static MyClass* theObject = nil;

    dispatch_once(&pred, ^{ theObject = [[self alloc] init]; });
    return theObject;
}

我一直抵制 GCD 模式,因为在我看来,正在发生的事情看起来不太明显.但是,没有什么是评论无法解决的!与@synchronized 相比,GCD 使用的锁更轻量级,因此会更快.

I've always resisted the GCD pattern because to my eye it looks less obvious what is happening. However, that's nothing that can't be fixed with a comment! The locks used by GCD are more lightweight in comparison to @synchronized, so this will be faster.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcgkhbh
系列文章
更多 icon
同类精品
更多 icon
继续加载