代表的非保留数组 [英] Non-retaining array for delegates

查看:147
本文介绍了代表的非保留数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Cocoa Touch项目中,我需要一个特定的类,不仅有一个委托对象,而且还有很多。

In a Cocoa Touch project, I need a specific class to have not only a single delegate object, but many of them.

看起来我应该创建一个NSArray为这些代表;
问题是NSArray将保留所有这些委托,它不应该(通过约定对象不应该保留他们的委托)。

It looks like I should create an NSArray for these delegates; the problem is that NSArray would have all these delegates retained, which it shouldn't (by convention objects should not retain their delegates).

我自己的数组类防止保留或有更简单的方法?
谢谢!

Should I write my own array class to prevent retaining or are there simpler methods? Thank you!

推荐答案

我之前发现这段代码)。

I found this bit of code awhile ago (can't remember who to attribute it to).

这是相当ingenius,使用类别允许创建一个可变的数组,没有保留/释放,支持 CFArray

It's quite ingenius, using a Category to allow the creation of a mutable array that does no retain/release by backing it with a CFArray with proper callbacks.

@implementation NSMutableArray (WeakReferences)
    + (id)mutableArrayUsingWeakReferences {
    return [self mutableArrayUsingWeakReferencesWithCapacity:0];
    }

    + (id)mutableArrayUsingWeakReferencesWithCapacity:(NSUInteger)capacity {
    CFArrayCallBacks callbacks = {0, NULL, NULL, CFCopyDescription, CFEqual};
    // We create a weak reference array
    return (id)(CFArrayCreateMutable(0, capacity, &callbacks));
    }
@end

EDIT 原创文章: http://ofcodeandmen.poltras.com

这篇关于代表的非保留数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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