for(NSString * ID in someNSDictionary) 将以什么顺序运行?
for(NSString * ID in someNSMutableDictionary)
{
//do something
}
现在,ID 会按照添加到 someNSMutableDictionary 的顺序显示吗?
for(NSString * ID in someNSMutableDictionary)
{
//do something
}
Now, will ID show up in the order they are added to someNSMutableDictionary?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,
someNSMutableDictionary
中键的顺序未定义。字典是关于键值对的,而不是关于顺序的。如果您需要以某种顺序迭代字典 - 对键进行排序,然后迭代有序键的数组并单独获取每个值。
No, the order of the keys in
someNSMutableDictionary
is undefined. Dictionaries are about key-value pairs, not about order.If you need to iterate over dictionary with some order - order the keys and then iterate over the array of ordered keys and fetch each value individually.
否,除非您对 NSDictionary 对象的键强制排序,否则 ID 不会按添加顺序或任何排序顺序显示。
键的随机排序是一种最大限度地减少通过键访问值所花费的时间的技术,通常用于大多数字典或哈希表实现中。
如果您希望键按排序顺序,可以使用以下代码(假设键采用简单的 ASCII 排序):
No the IDs do not show up in the orders they were added or in any sorted order unless you force a sort on the keys of an NSDictionary object.
Random ordering of keys is a technique to minimize the time taken to access a value by key and is generally used in most dictionary or hashtable implementations.
If you want the keys to be in sorted order you can use the following code (assuming simple ASCII sort for the keys):