创建具有订单支持的 NSDictionary
我需要一个 NSDictionary 对象,它不仅支持键>值存储,还支持像 Array 一样的键序列。
Objective-C 上有什么东西吗?
I need a NSDictionary
object that not only support key->value storage but also support key
sequence just like Array does.
Is there something exists on Objective-C ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Monolo 的答案是正确的方法。
[self allKeys]
不返回有序序列。它是一个NSSet
风格。我编写了一个名为 PLHash 的类,它具有许多功能:
这里是 URL
https://github.com/xhan/PlutoLand/blob/master /PlutoLand/Additions/PLHash.h
Monolo's answer is the right way to do this.
[self allKeys]
does not return ordered sequence. it's anNSSet
style.And I write a Class named
PLHash
that has many features:here is the URL
https://github.com/xhan/PlutoLand/blob/master/PlutoLand/Additions/PLHash.h
没有内置任何内容,但您可以使用字典旁边的键数组来记录顺序。
Nothing built in, but you can use an array of keys along side the dictionary to record the order.
您最好要么获取 allkeys 数组并对其进行排序,然后获取键的值,要么创建自己的类以您想要的方式存储对象。听起来你确实在做一些 NSDictionary 真正不是为了做的事情,你想要做什么,你想要对键进行排序?
You would be best off either getting the allkeys array and sorting that then getting the value for a key or making your own class to store objects the way you want them. It really sounds like you are doing something NSDictionary really wasn't built to do, what are you trying to do that you want the keys sorted?
一种选择是在 nsdictionary 上创建一个类别,将其称为排序键或其他名称,它返回排序的 [self allKeys]。
更新:现在我没有从手机上回复,这是我的建议,为了清晰起见,写得很详细,并假设您正在使用 ARC:
我个人认为子类化 NSDictionary 不是正确的方法。
One option is to create a category on nsdictionary, call it sortedKeys or something, which returns a sorted [self allKeys].
Update: Now that I'm not responding from my phone, here's my suggestion, written verbosely for clarity, and assuming you're using ARC:
I personally don't think that subclassing NSDictionary is the way to go.