NSDictionary 中的键和值是否有序? [英] Are keys and values in an NSDictionary ordered?

查看:21
本文介绍了NSDictionary 中的键和值是否有序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思是:NSDictionary 中的键和值的顺序是否总是与初始化 NSDictionary 时指定的顺序相同?或者如果我真的需要知道键的顺序,我应该更好地维护一个单独的 NSArray 吗?

I mean: Is the order of keys and values in an NSDictionary always the same like how they were specified when initializing the NSDictionary? Or should I better maintain a seperate NSArray if I really need to know the order of keys?

推荐答案

不,它们不是有序的.只要不添加或删除字典中的任何元素,它们就会保持相同的顺序,但是一旦添加或删除元素,新的顺序就会完全不同.

No, they are not ordered. As long as you don't add or remove any elements from the dictionary, they will remain in the same order, but as soon as you add or remove an element, the new order will be completely different.

如果您需要对键/值进行排序,请改用 NSArray(或其他一些有序数据结构).

If you need the keys/values to be ordered, use an NSArray (or some other ordered data structure) instead.

这篇关于NSDictionary 中的键和值是否有序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆