像输入一样对 NSMutableDictionary 的输出进行排序
如何按照输入的顺序输出字典的值。
示例:
我的输入:
[dicValue0 setObject:@"Start Date & Time" forKey:@"START_DATETIME"];
[dicValue0 setObject:@"Specify End" forKey:@"SPECIFY_END"];
[dicValue0 setObject:@"End Date & Time" forKey:@"END_DATETIME"];
[dicValue0 setObject:@"Open End" forKey:@"END_OPEN"];
输出:
- 开始日期和日期时间
- 结束日期和时间时间
- 指定结束
- 开放结束
我知道字典如何有效,但我希望输出的顺序与输入的顺序相同!
我可以编写一个循环,按照输入的顺序对输出进行排序。但如果我有 10000 多个值,那不是最好的、高性能的方法。 苹果有什么东西可以帮助我解决这个问题吗?
how is it possbile to output the values of a dictionary in the order of input.
Example:
My Input:
[dicValue0 setObject:@"Start Date & Time" forKey:@"START_DATETIME"];
[dicValue0 setObject:@"Specify End" forKey:@"SPECIFY_END"];
[dicValue0 setObject:@"End Date & Time" forKey:@"END_DATETIME"];
[dicValue0 setObject:@"Open End" forKey:@"END_OPEN"];
Outputs:
- Start Date & Time
- End Date & Time
- Specify End
- Open End
I know how a dictionary works, but I want the output in the same order as the input!
I can write a loop which sorts me the output in the order of the input. But if i had 10000+ values that's not the best and performant way.
Is there anything from apple, that helps me with this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该创建一个数组来跟踪插入键的顺序,然后迭代该数组以从字典中提取值。 Foundation 中没有内置的方法来拥有有序字典。
You should create an array that keeps track of the order of the inserted keys, then iterate through that to pull values out of the dictionary. There's no built-in way to have an ordered dictionary in Foundation.
我很确定字典没有跟踪输入顺序。你如何输出字典,循环按键或只是打印字典?
如果您知道要检索对象的顺序,则可以创建自己版本的键数组并循环遍历该数组,以按照所需的顺序从字典中提取对象
I am pretty sure that dictionaries are not keeping track of the input order. How are you outputting the dictionary, looping through keys or just printing the dictionary?
If you know the order you want to retrieve the objects, you can create your own version of the keys array and loop through that to pull out the objects from the dictionary in your desired order