Objective-C 中的哈希表
我可以使用什么类将 NSMutableIndexSet 中的索引关联到对象?
What class can I use to associate an index in a NSMutableIndexSet to an object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NSDictionary
或NSMutableDictionary
。您可以使用对象作为键。还有
NSHashTable
。NSDictionary
orNSMutableDictionary
. You can use objects as it's keys.There is also
NSHashTable
.如果您想使用原始整数作为对象的键,可以使用
CFMutableDictionaryRef
代替。这会让你从 Cocoa 下降到 CoreFoundation,但它仍然可以正常工作:如果你要经常访问这本字典并且不想处理一大堆字典,这真的很方便。瞬态 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: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)您可能想使用
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 anNSNumber
via-[NSNumber numberWithUnsignedInteger:]
for the keys (this like manual boxing of primitives in Java) and add a key-value pair using-[NSMutableDictionary addObject:forKey:]
.