如何按现有顺序从字典中获取所有键的数组?
可能的重复:
如何顺序访问字典?
我有一本字典,我想要以与字典中存在的相同顺序重新获取数组中的所有键。 NSDictionary 的“allKeys”方法的文档说数组中返回对象的顺序未定义。可以做什么来按照与字典相同的顺序获取键。
预先非常感谢!
Possible Duplicate:
How to sequentialy access a dictionary??
I have a dictionary , I want to reitrive all keys in an array with the same order which exist inside the dictionary . The documentation on "allKeys" method of NSDictionary says the order of returned objects in array is not defined . What can be done to get the keys in the same order as that of the dictionary .
Thanks a lot in advance !!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,您提出的问题很好地总结了问题,但是提供的解决方案对于您的需求来说可能有点太复杂(如果您不想/没有时间编写子类或您自己的数据结构)。
快速而肮脏的替代方案很简单,就是拥有一个数组,其中包含您想要访问的所有键,按照您想要访问的顺序排列。然后,您可以简单地将这个数组与您的字典匹配,以按照正确的顺序提取值。
So the question you were pointed to summarises the issue nicely, but the solutions offered could be a little bit too complex for your needs (if you didn't want / have the time to write a sub-class or your own data structure).
The quick and dirty alternative is simple to have an array that contains all the keys you want to access in the order you want to access them in. You can then simply match this array back to your dictionary to pull out values in the correct order.