带有弱引用的 NSMutableDictionary:使用 CFRetain 作为回调时发出警告
我正在尝试创建一个可变字典,该字典具有值对象的弱引用(键行为正常)。 这就是我尝试这样做的方式:
+ (id)mutableDictionaryUsingWeakReferencesWithCapacity:(NSUInteger)capacity
{
CFDictionaryKeyCallBacks keyCallbacks = {0, CFRetain, CFRelease, CFCopyDescription, CFEqual, CFHash};
CFDictionaryValueCallBacks valueCallbacks = {0, NULL, NULL, CFCopyDescription, CFEqual};
id<NSObject> obj = (id)(CFDictionaryCreateMutable(NULL, capacity, &keyCallbacks, &valueCallbacks));
return [obj autorelease];
}
不幸的是,我在声明 keyCallbacks 时收到警告(来自不兼容指针类型的初始化),并且我已将其跟踪到使用 CFRetain 和 CFRelease。由于某种原因,这些回调与所需的原型不匹配(CFDictionaryRetainCallback 和 CFDictionaryReleaseCallback)
在文档中,它说示例 CFDictionaryRetainCallback 应该如下所示:
const void *MyCallBack (
CFAllocatorRef allocator,
const void *value
);
但是现有的 CFRetain 被声明为
CFTypeRef CFRetain(CFTypeRef cf);
它缺少分配器参数,这就是为什么我认为编译器给出警告:它与函数签名不完美匹配。
有人尝试过做这样的事情吗?
I'm trying to create a mutable dictionary that has weak-references for the value objects (the keys behave normally).
This is how i'm trying to do it:
+ (id)mutableDictionaryUsingWeakReferencesWithCapacity:(NSUInteger)capacity
{
CFDictionaryKeyCallBacks keyCallbacks = {0, CFRetain, CFRelease, CFCopyDescription, CFEqual, CFHash};
CFDictionaryValueCallBacks valueCallbacks = {0, NULL, NULL, CFCopyDescription, CFEqual};
id<NSObject> obj = (id)(CFDictionaryCreateMutable(NULL, capacity, &keyCallbacks, &valueCallbacks));
return [obj autorelease];
}
Unfortunately I get a warning (Initialization from incompatible pointer type)in when declaring the keyCallbacks, and i've tracked it down to using CFRetain and CFRelease. For some reason these callbacks do not match the required prototypes (CFDictionaryRetainCallback and CFDictionaryReleaseCallback)
In the documentation it says that an example CFDictionaryRetainCallback should look something like this:
const void *MyCallBack (
CFAllocatorRef allocator,
const void *value
);
But the existing CFRetain is declared as
CFTypeRef CFRetain(CFTypeRef cf);
It's missing the allocator parameter and that's why I think the compiler gives a warning: it's not a perfect match in the signature of the function.
Has anybody tried to do something like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要那样做。使用 NSMapTable。
Don’t Do That. Use
NSMapTable
.如果您只想要默认的
CFRetain
/CFRelease
行为,这应该可行:保留回调应该很容易从那里实现。
if you just want the default
CFRetain
/CFRelease
behaviour, this should work:the retain callback should be easy to implement from there.
我设法使用 kCFTypeDictionaryKeyCallBacks 常量使其工作,而不是手动声明关键回调。
代码现在看起来像这样:
但是,我仍然很好奇为什么我的初始代码不起作用
I managed to get it working using the kCFTypeDictionaryKeyCallBacks constant instead of manually declaring the key callbacks.
The code now looks like this:
However, i'm still curious why isn't my initial code working
如果您不介意稍微玩一下运行时,这就是我正在做的事情对于我的一个项目(它可以在 ATM 上运行,但有点马虎)。它动态地为您添加的任何对象创建一个新的子类,并将该对象的类设置为该子类。子类保留一个对象数组,每当对象被释放时就应该通知这些对象。字典将自身添加到该数组中,以便在对象被释放时可以将其删除。
If you don't mind playing with the runtime a bit, this is something I'm working on for a project of mine (it works ATM but it's a bit sloppy). It dynamically creates a new subclass of any object you add and set that object's class to the subclass. The subclass keeps an array of objects that should be notified whenever the object is deallocated. The dictionary adds itself to this array so that it can remove the object if it's ever deallocated.