NSMapTable 和 NSMutableDictionary 的区别

发布于 2024-11-27 18:39:38 字数 101 浏览 1 评论 0 原文

除了允许键为指针之外,NSMapTableNSMutableDictionary 相同吗?

内存管理有什么不同吗?

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

Does it differ in memory management?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谜兔 2024-12-04 18:39:38

NSMapTable 比 NSDictionary 更灵活。虽然 NSDictionary 保留值的强引用并复制键,但您可以将 NSMapTable 配置为具有独立于对象和值的任何行为:强、弱或复制(存在更多行为选项)。


一个实际用例:NSDictionary 保留值指针的强引用(保留),但复制键。这意味着 a) 关键实例必须实现 NSCopying 协议,b) 根据类的复杂性,复制可能会增加开销。另一方面,您可以将 NSMapTable 配置为像 NSDictionary 一样使用值和键的强引用,无需复制或 NSCopying 协议。

以前可以使用一个对象到对象的行为来模拟
NSDictionary 如果所有键都是包含内存的 NSNumbers
映射中源对象的地址(别笑,我见过
完成)但在这个运行之外,NSMapTable 提供了一个真正的
Cocoa 集合中首次实现对象到对象的映射
类。

(摘自一篇介绍 NSMapTable 的精彩文章< /a>.)


让我们看一下 API。这将返回一个与 NSMutableDictionary 工作方式大致相同的对象:

[NSMapTable mapTableWithKeyOptions:NSMapTableCopyIn
                      valueOptions:NSMapTableStrongMemory]

这将返回一个工作的对象,但不会复制键:

[NSMapTable mapTableWithKeyOptions:NSMapTableStrongMemory
                      valueOptions:NSMapTableStrongMemory]

注意:看起来 NSMapTable API 在最近的 SDK 中已经发生了变化,但这种语法似乎与所有 SDK。

NSMapTable 可在 OS X 10.5+ 和 iOS 6.0+ 上使用。

NSMapTable is more flexible than NSDictionary. While NSDictionary keeps strong references for values and copies the keys, you can configure NSMapTable to have any of those behaviors independently for objects and values: strong, weak or copy (more behavior options exist).


A practical use case: an NSDictionary keeps a strong reference (retains) of the pointer of the value, but copies the key. This means a) the key instance must implement the NSCopying protocol and b) depending on the complexity of the class, copying might add an overhead. On the other hand, you can configure an NSMapTable to act like an NSDictionary that uses strong references for both the values and the keys, no copying or NSCopying protocol needed.

An object-to-object behavior could previously be emulated using an
NSDictionary if all the keys were NSNumbers containing the memory
address of the source object in the mapping (don't laugh, I've seen it
done) but outside of this run-around, NSMapTable offers a true
object-to-object mapping for the first time in a Cocoa collection
class.

(From a great article covering NSMapTable when it was introduced.)

Let's look at the API. This will return an object that works much the same as an NSMutableDictionary:

[NSMapTable mapTableWithKeyOptions:NSMapTableCopyIn
                      valueOptions:NSMapTableStrongMemory]

This will return an object that works doesn't copy the keys:

[NSMapTable mapTableWithKeyOptions:NSMapTableStrongMemory
                      valueOptions:NSMapTableStrongMemory]

Note: It looks like the NSMapTable API has changed in recent SDKs, but this syntax seems to be compatible with all SDKs.

NSMapTable is available on OS X 10.5+ and iOS 6.0+.

裂开嘴轻声笑有多痛 2024-12-04 18:39:38

或多或少,它有一些额外的选项,如果您使用垃圾收集(我猜这已经被弃用了),这些选项主要是相关的。如果不使用垃圾收集,内存管理要求是相同的。

另一个区别是 NSMapTable 可以选择使用指针相等进行散列。

More or less, it has some additional options that are primarily relevant if you use Garbage Collection (which is sort of deprecated, I guess). If you don't use Garbage Collection, the memory management requirements are the same.

Another difference is that NSMapTable can optionally use pointer equality for hashing.

走走停停 2024-12-04 18:39:38

请注意,如果使用弱-弱、弱-强或强-弱绑定,NSMapTable 有时不会释放键和对象 http://cocoamine.net/blog/2013/12/13/nsmaptable-and-zeroing-weak-references/

同样在 NSMapTable.h 中,您可以发现“当弱键被回收时,条目不一定会立即清除”

+ (id)weakToStrongObjectsMapTable NS_AVAILABLE(10_8, 6_0); 
// entries are not necessarily purged right away when the weak key is reclaimed
+ (id)weakToWeakObjectsMapTable NS_AVAILABLE(10_8, 6_0); 
// entries are not necessarily purged right away when the weak key or object is reclaimed

Be aware that NSMapTable sometimes not deallocates keys and objects if weak-weak, weak-strong or strong-weak bindings are used http://cocoamine.net/blog/2013/12/13/nsmaptable-and-zeroing-weak-references/.

Also in NSMapTable.h you can find that 'entries are not necessarily purged right away when the weak key is reclaimed' :

+ (id)weakToStrongObjectsMapTable NS_AVAILABLE(10_8, 6_0); 
// entries are not necessarily purged right away when the weak key is reclaimed
+ (id)weakToWeakObjectsMapTable NS_AVAILABLE(10_8, 6_0); 
// entries are not necessarily purged right away when the weak key or object is reclaimed
成熟稳重的好男人 2024-12-04 18:39:38

NSMapTable 和 NSMutableDictionary 之间的主要区别在于 NSMapTable 存储弱指针。这意味着当您像这样调用时:

[my_table setValue: val forKey: key];

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

因此,如果您正在使用垃圾收集,并且不需要担心对象的保留计数,那么您可能需要使用 NSMapTable。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文