Objective-C 中的哈希表

发布于 2024-10-24 19:41:33 字数 45 浏览 1 评论 0原文

我可以使用什么类将 NSMutableIndexSet 中的索引关联到对象?

What class can I use to associate an index in a NSMutableIndexSet to an object?

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

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

发布评论

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

评论(3

逆蝶 2024-10-31 19:41:33

NSDictionaryNSMutableDictionary。您可以使用对象作为键。

还有NSHashTable

NSDictionary or NSMutableDictionary. You can use objects as it's keys.

There is also NSHashTable.

懒猫 2024-10-31 19:41:33

如果您想使用原始整数作为对象的键,可以使用 CFMutableDictionaryRef 代替。这会让你从 Cocoa 下降到 CoreFoundation,但它仍然可以正常工作:

CFMutableDictionaryRef indexMap;

indexMap = CFDictionaryCreateMutable(NULL, 0, NULL, &kCFTypeDictionaryValueCallBacks);

NSUInteger key = 42;
id value = @"The Answer";
CFDictionarySetValue(indexMap, (const void *)key, value);

id value = CFDictionaryGetValue(indexMap, (const void *)key);
CFRelease(indexMap);

如果你要经常访问这本字典并且不想处理一大堆字典,这真的很方便。瞬态 NSNumber 对象。

(我忽略了你可以免费桥接这个问题,因为一旦你开始研究键和值的行为,你根本就不想将其视为 NSMutableDictionary )

If you want to use a raw integer as the key to an object, you can use a CFMutableDictionaryRef instead. This is going to drop you down a layer from Cocoa into CoreFoundation, but it'll still work just fine:

CFMutableDictionaryRef indexMap;

indexMap = CFDictionaryCreateMutable(NULL, 0, NULL, &kCFTypeDictionaryValueCallBacks);

NSUInteger key = 42;
id value = @"The Answer";
CFDictionarySetValue(indexMap, (const void *)key, value);

id value = CFDictionaryGetValue(indexMap, (const void *)key);
CFRelease(indexMap);

This is really handy if you're going to be accessing this dictionary frequently and don't want to deal with a whole bunch of transient NSNumber objects.

(I'm ignoring that you can toll-free bridge this, because once you start mucking around with the behaviors of keys and values, you don't really want to consider this an NSMutableDictionary at all)

温柔嚣张 2024-10-31 19:41:33

您可能想使用 NS[Mutable]Dictionary。您可以通过键的 -[NSNumber numberWithUnsignedInteger:] 将整数索引包装在 NSNumber 中(这类似于 Java 中基元的手动装箱),并添加键值对使用 -[NSMutableDictionary addObject:forKey:]

You probably want to use an NS[Mutable]Dictionary. You can wrap the integer indexes in an NSNumber via -[NSNumber numberWithUnsignedInteger:] for the keys (this like manual boxing of primitives in Java) and add a key-value pair using -[NSMutableDictionary addObject:forKey:].

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