ARC下归零弱引用的集合 [英] Collections of zeroing weak references under ARC

查看:213
本文介绍了ARC下归零弱引用的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得ARC下的归零弱引用数组?我不想让数组保留对象。并且我想要数组元素在被释放时删除自己,或者将这些条目设置为nil。

How can I get an array of zeroing weak references under ARC? I don't want the array to retain the objects. And I'd like the array elements either to remove themselves when they're deallocated, or set those entries to nil.

同样,我如何使用字典?我不想让字典保留值。再次,我想要字典元素要么在值被释放时删除自己,要么将值设置为nil。 (我需要保留密钥,这是唯一的标识符,至少直到相应的值被释放。)

Similarly, how can I do that with a dictionary? I don't want the dictionary to retain the values. And again, I'd like the dictionary elements either to remove themselves when the values are deallocated, or set the values to nil. (I need to retain the keys, which are the unique identifiers, at least until the corresponding values are deallocated.)

这两个问题涵盖类似的地面:

These two questions cover similar ground:

  • NSArray of weak references to objects under ARC
  • Having a list of unretained id objects?

根据文档,NSPointerArray和NSHashMap都不支持ARC下的弱引用。 NSValue的nonretainedObjectValue也不会工作,因为它是非零的。

Per the documentation, neither NSPointerArray nor NSHashMap support weak references under ARC. NSValue's nonretainedObjectValue will not work either, as it is non-zeroing.

我看到的唯一解决方案是创建我自己的 NSValue类包装具有(weak)属性的类,因为此回答提到,接近结束。有没有更好的方式,我没有看到?

The only solution I see is to create my own NSValue-like wrapper class with a (weak) property, as this answer mentions, near the end. Is there a better way I'm not seeing?

我正在开发OS X 10.7和iOS 6.0。

I'm developing for OS X 10.7 and iOS 6.0.

推荐答案

这里是一个归零弱引用包装类的代码。它可以正确地与NSArray,NSSet和NSDictionary。

Here's code for an a zeroing weak-referencing wrapper class. It works correctly with NSArray, NSSet, and NSDictionary.

这个解决方案的优点是它与旧的操作系统兼容,这很简单。缺点是当迭代时,你可能需要验证 -nonretainedObjectValue 是非nil之前使用它。

The advantage of this solution is that it's compatible with older OS's and that's it simple. The disadvantage is that when iterating, you likely need to verify that -nonretainedObjectValue is non-nil before using it.

WeakReference.h 这是Cocoanetics回答第一部分中的包装器,它使用块来完成同样的事情。 >

WeakReference.h

@interface WeakReference : NSObject {
    __weak id nonretainedObjectValue;
    __unsafe_unretained id originalObjectValue;
}

+ (WeakReference *) weakReferenceWithObject:(id) object;

- (id) nonretainedObjectValue;
- (void *) originalObjectValue;

@end

WeakReference.m / p>

WeakReference.m

@implementation WeakReference

- (id) initWithObject:(id) object {
    if (self = [super init]) {
        nonretainedObjectValue = originalObjectValue = object;
    }
    return self;
}

+ (WeakReference *) weakReferenceWithObject:(id) object {
    return [[self alloc] initWithObject:object];
}

- (id) nonretainedObjectValue { return nonretainedObjectValue; }
- (void *) originalObjectValue { return (__bridge void *) originalObjectValue; }

// To work appropriately with NSSet
- (BOOL) isEqual:(WeakReference *) object {
    if (![object isKindOfClass:[WeakReference class]]) return NO;
    return object.originalObjectValue == self.originalObjectValue;
}

@end

这篇关于ARC下归零弱引用的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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