对于唯一键和唯一值,有 NSDictionary 的替代品吗?

发布于 2024-08-21 20:10:38 字数 439 浏览 8 评论 0原文

我正在编写一些 Cocoa 类来解析 MP3 文件中的 ID3 标签。为了使它们尽可能易于使用,我允许选择通过实际 ID3 帧 ID(“TCON”、“TPE1”、“TALB”等)或等效单词/短语(“ 为了存储这些数据,目前我有一个引用类,

它返回一个 NSDictionary,其中帧 id 作为键,单词/短语作为对象。由于我需要在两个方向查找定义,目前我有第二种方法,它返回字典“交换”,因此单词/短语是键。

我的问题是是否有更好的方法来表示这些数据。理想情况下,会有类似于 NSDictionary 的东西,区别在于键和值都必须是唯一的,并且您可以查找“objectForKey:”和“keyForObject:”

我可以自己为此编写一个类,但是正如 NSDictionary 文档中所述,我可能会失去哈希表的一些效率......而且我宁愿在整体实现中保持尽可能低的类数量。

有什么想法吗?干杯。

I'm in the middle of writing some Cocoa classes to parse ID3 tags from MP3 files. To make them as easy to use as possible, I'm allowing the option to request a tag by the actual ID3 frame id ("TCON", "TPE1", "TALB", etc.) or an equivalent word/phrase ("genre", "artist", "album", etc.)

To store this data, currently I have a reference class which returns an NSDictionary with the frame id's as keys, and word/phrases as objects. As I need to look up definitions in both directions, currently I have a second method which returns the dictionary 'switched round', so the words/phrases are the keys.

My question is whether there is a better way to represent this data. Ideally there would be something similar to NSDictionary, the difference being that both the keys and the values must be unique, and you could look up both an "objectForKey:" and a "keyForObject:"

I could write a class for this myself, but I may lose some of the efficiency from hash tables as described in the NSDictionary documentation... also I'd rather keep the number of classes as low as possible in the overall implementation.

Any ideas? Cheers.

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

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

发布评论

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

评论(1

野稚 2024-08-28 20:10:38

有趣的是,你应该问这个...

Quinn TaylorCHDataStructures 框架 刚刚添加了CHBidirectionary 上周添加到了框架。它允许您按键查找对象,并按对象查找键。它基本上是两个可变字典的包装,因此可以保证与常规字典相同的查找时间。

唯一需要注意的是对象和键都必须符合 NSCopying 协议。

Funny you should ask this...

Quinn Taylor, the author of the CHDataStructures framework just added a CHBidirectionalDictionary to the framework last week. It allows you to find objects by key, and find keys by object. It's basically a wrapper around two mutable dictionaries, so you're guaranteed the same lookup time as with a regular dictionary.

The only caveat is that both the object and key must both conform to the NSCopying protocol.

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