Is NSDictionary key order guaranteed the same as initialized if it never changes?

江枫思渺然 提交于 2019-11-28 12:19:43

No, the keys are not likely to appear in the same order as your plist file. I can't look under the hood, but I would guess that the keys are ordered in whatever way that provides an efficient look-up. The documentation for allKeys says that the order of the elements in the array is not defined.

If you need an NSDictionary that maintains order, take a look at CHDataStructure's CHOrderedDictionary, which does exactly that. I use it all the time.

I would not assume that the keys will be in any kind of order because allKeys does not guarantee order.

Internally, NSDictionary uses a hash table.

From allKeys description:

The order of the elements in the array is not defined.

If you want to display values in some order then get the keys, sort the keys array and then get the value for each key.

Nothing at all is guaranteed about order. It’s not even guaranteed that iterating over a dictionary twice will give you the keys in the same order.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!