Swift:如何按照添加顺序遍历字典的键

Swift: How to go through keys of a dictionary in the order they were added

for key in Array(dictionaryName.keys) returns 键随机排列。 有没有办法按照将键添加到字典中的顺序取出键?

为此,您必须在构建字典时将键存储在数组中。

字典没有顺序。

没有。根据定义,字典是无序的。例如,您可以尝试使用 NSOrderedSet。

在内部,NSDictionary 使用散列 table。 来自 NSDictionary 描述 (https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/):

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