一个不复制 iPhone 3.0 SDK 密钥的 NSDictionary?
我使用 NSDictionary 作为关联数组(即我使用的键可以是任何任意对象)。 NSDictionary 非常烦人的事情之一是它总是复制密钥并存储它。在我的场景中,我稍后将从 NSDictionary 中检索密钥并使用它们执行一些操作。该操作恰好取决于键的对象标识。因为我后来检索到的密钥是我最初用作密钥的对象的副本。后面的对象身份检查失败。
我的问题是,iPhone 3.0 SDK 中是否有类似哈希表的数据结构不复制密钥?谢谢。
过时的男孩
I am using NSDictionary as an associated array (i.e, the keys i am using can be any arbitrary objects). One of the very annoying thing about NSDictionary is that it always make a copy of the key and store it. In my scenario, I will later retrieve the keys from the NSDictionary and do some operations with them. The operation happens to depend on the object identity of the keys. Because the keys i retrieved later are copies of the objects i originally used as keys. The later object identity check fails.
My question is, is there any hashtable-like data structure in the iPhone 3.0 SDK that doesn't make copy of the keys? Thank you.
Outdateboy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不希望复制(甚至保留)密钥,可以使用 CFDictionary 并提供 kCFTypeDictionaryKeyCallbacks 或 NULL 或自定义关键回调。
要检查对象是否相等,您应该使用
-isEqual:
而不是==
。If you don't want your key to be copied (or even retained), you can use
CFDictionary
and supply akCFTypeDictionaryKeyCallbacks
orNULL
or customized key callbacks.To check if objects are equal you should use
-isEqual:
instead of==
.NSDictionary 与 CFDictionary 是免费桥接的。所以就这样做:
只有 Cocoa 方法复制密钥。
NSDictionary is toll-free bridged with CFDictionary. So just do:
It's only the Cocoa method that copies the key.
我最近做了以下工作来实现一个不复制其键的 MutableDictionary。
如果您的键不遵守 NSCopying 并且您希望在设置键/值时避免编译器警告,请使用:
I've recently done the following to achieve a MutableDictionary that doesn't copy it's keys.
If you're keys don't obey NSCopying and you want to avoid compiler warnings when setting key/values use: