xcode NSDictionary allKeys - 它是否总是返回相同的顺序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9575387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:32:19  来源:igfitidea点击:

NSDictionary allKeys - does it always return the same order?

iphoneobjective-cxcodecocoa-touchnsdictionary

提问by KerrM

I'm fairly new to Objective-C and I was working with NSDictionaries today and came across the allKeys method. As I understand, it returns an NSArray containing the keys for the dictionary in a random order. However, is this order always the same? ie, if I call allKeys on the same dictionary 20 times in a row am I guaranteed to get the same order of results?

我是 Objective-C 的新手,今天我在使用 NSDictionaries 并遇到了 allKeys 方法。据我了解,它以随机顺序返回一个包含字典键的 NSArray。然而,这个顺序总是一样的吗?即,如果我在同一个字典上连续调用 allKeys 20 次,我能保证得到相同顺序的结果吗?

Thanks,

谢谢,

采纳答案by Lily Ballard

If you call the method 20 times in a row on a dictionary that is not being mutated, it most likelywill return the same order. That said, I would strongly discourage you from relying on this, as it's highly dependent upon implementation details. The only reason I'm saying that it will most likely return the same order is because accessing a dictionary should notmutate any internal structures, and in the absence of mutation, the only other way to get a different order would be to explicitly introduce nondeterminism, i.e. relying on global state, such as a random number generator or the CPU's clock, and I find it extremely doubtful that NSDictionarydoes any of this.

如果您在未突变的字典连续调用该方法 20 次,它很可能会返回相同的顺序。也就是说,我强烈建议您不要依赖它,因为它高度依赖于实现细节。我说它很可能会返回相同顺序的唯一原因是因为访问字典不应该改变任何内部结构,并且在没有突变的情况下,获得不同顺序的唯一其他方法是显式引入非确定性,即依赖于全局状态,例如随机数生成器或 CPU 的时钟,我觉得这其中的NSDictionary任何一个都非常值得怀疑。

If you need to ensure the same ordering of keys, you should probably just sort them once you fetch them from the dictionary.

如果您需要确保键的顺序相同,您可能应该在从字典中获取它们后对它们进行排序。

回答by peterept

NSDictionary objects are unordered to maintain performance.

NSDictionary 对象是无序的以保持性能。

If you need an ordered dictionary, look at this project:

如果你需要一个有序的字典,看看这个项目:

http://code.google.com/p/cocoa-sorted-dictionary/

http://code.google.com/p/cocoa-sorted-dictionary/

回答by Andrew Madsen

The way to figure this out is to write a quick test program. Really, you should never rely on non-guaranteed, non-documented results like this. It's bad programming practice, and is fairly likely to break with a new OS release.

解决这个问题的方法是编写一个快速测试程序。真的,你永远不应该依赖像这样的非保证、非文档化的结果。这是一种糟糕的编程习惯,很可能会因新的操作系统版本而中断。

If you need the keys that come back to be in a particular order, you should sort them after retrieving them. You can do that with the -[NSArray sortedArrayUsing...]group of methods.

如果您需要按特定顺序返回的键,则应在检索它们后对它们进行排序。你可以用一-[NSArray sortedArrayUsing...]组方法来做到这一点。