NSMapTable和NSMutableDictionary的区别 [英] NSMapTable and NSMutableDictionary differences

查看:117
本文介绍了NSMapTable和NSMutableDictionary的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了允许键为指针外, NSMapTable NSMutableDictionary 相同吗?

$

解决方案

NSMapTable和NSMutableDictionary之间的主要区别是NSMapTable存储弱指针。这意味着当你像这样调用smth:

  [my_table setValue:val forKey:key]; 

值和键不保留(这意味着没有保留消息发送给它们)。这就是为什么你可以使用任何对象(或者可能不是对象,但任何指针),因为他们不必回应保留消息。



所以你可能想使用NSMapTable如果你使用垃圾回收,你不需要关心对象的保留计数。


Is an NSMapTable the same as an NSMutableDictionary except for allowing keys to be pointers?

Does it differ in memory management?

解决方案

The main difference between NSMapTable and NSMutableDictionary is that NSMapTable stores weak pointers. This means that when you call smth like this:

[my_table setValue: val forKey: key];

the value and key are not retained (it means no retain message is sent to them). That's why you can use any object (or maybe not object but any pointer) cause they don't have to respond to retain message.

So you probably want to use NSMapTable if you're using garbage collection where you don't need to bother about retain count of an object.

这篇关于NSMapTable和NSMutableDictionary的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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