一个不复制 iPhone 3.0 SDK 密钥的 NSDictionary?

发布于 2024-08-19 06:51:14 字数 247 浏览 4 评论 0原文

我使用 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 技术交流群。

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

发布评论

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

评论(3

忘羡 2024-08-26 06:51:14

如果您不希望复制(甚至保留)密钥,可以使用 CFDictionary 并提供 kCFTypeDictionaryKeyCallbacks 或 NULL 或自定义关键回调。

要检查对象是否相等,您应该使用 -isEqual: 而不是 ==

If you don't want your key to be copied (or even retained), you can use CFDictionary and supply a kCFTypeDictionaryKeyCallbacks or NULL or customized key callbacks.

To check if objects are equal you should use -isEqual: instead of ==.

优雅的叶子 2024-08-26 06:51:14

NSDictionary 与 CFDictionary 是免费桥接的。所以就这样做:

CFDictionarySetValue((CFDictionaryRef)myMutableDict, key, object);

只有 Cocoa 方法复制密钥。

NSDictionary is toll-free bridged with CFDictionary. So just do:

CFDictionarySetValue((CFDictionaryRef)myMutableDict, key, object);

It's only the Cocoa method that copies the key.

满意归宿 2024-08-26 06:51:14

我最近做了以下工作来实现一个不复制其键的 MutableDictionary。

-(NSMutableDictionary*)mutableDictionaryWithRetainedKeys {
    CFMutableDictionaryRef dictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
    return CFBridgingRelease(dictionary);
}

如果您的键不遵守 NSCopying 并且您希望在设置键/值时避免编译器警告,请使用:

CFDictionarySetValue(dictionary, (__bridge const void *)(key), (__bridge const void *)(value));

I've recently done the following to achieve a MutableDictionary that doesn't copy it's keys.

-(NSMutableDictionary*)mutableDictionaryWithRetainedKeys {
    CFMutableDictionaryRef dictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
    return CFBridgingRelease(dictionary);
}

If you're keys don't obey NSCopying and you want to avoid compiler warnings when setting key/values use:

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