Swift 如果无论运行多少次内容都没有改变,字典是否会保留相同的顺序?
Does a Swift Dictionary preserve same order if content is not changed regardless the number of runs?
根据
中回答的问题
A dictionary stores associations between keys of the same type and values of the same type in an collection with no defined ordering.
因此,
//First Attempt
var dict : Dictionary = ["name1" : "Loy", "name2" : "Roy"]
println(dict)
//output:
[name2: Roy, name1: Loy]
//Second Attempt
var dict : Dictionary = ["name2" : "Loy", "name1" : "Roy"]
println(dict)
//output:
[name2: Loy, name1: Roy]
我的问题是,
是否保证词典内容不改顺序不变?
例如上面的例子,无论运行多少次,第一次尝试总是returns相同的顺序?
不,不能保证。没有订单就意味着没有订单。不要试图依赖未记录的事故。事实上,Apple 明确警告说,在现代 Swift 中,顺序是随机的,并且对于不同的运行可能会有所不同。
在您的示例中,您只有一个文字,您可以通过转换为 KeyValuePairs.
来获得有保证的顺序
只要不修改字典,key/value 对的顺序就不会改变。其他一切都未指定。来自 documentation(强调已添加):
The order of key-value pairs in a dictionary is stable between mutations but is otherwise unpredictable.
注:这仅适用于printing/enumerating单个程序中的字典运行。 As @ matt 说,每个节目的顺序是随机的 运行。这是因为 Hasher
是随机的:
Do not save or otherwise reuse hash values across executions of your program. Hasher is usually randomly seeded, which means it will return different values on every new execution of your program. The hash algorithm implemented by Hasher may itself change between any two versions of the standard library.
哈希随机化在 Swift 4.2 中强制实施,实施 SE 0206 Hashable Enhancements:
To implement nondeterminism, Hasher uses an internal seed value initialized by the runtime during process startup. The seed is usually produced by a random number generator, but this may be disabled by defining the SWIFT_DETERMINISTIC_HASHING environment variable with a value of 1 prior to starting a Swift process.
根据
A dictionary stores associations between keys of the same type and values of the same type in an collection with no defined ordering.
因此,
//First Attempt
var dict : Dictionary = ["name1" : "Loy", "name2" : "Roy"]
println(dict)
//output:
[name2: Roy, name1: Loy]
//Second Attempt
var dict : Dictionary = ["name2" : "Loy", "name1" : "Roy"]
println(dict)
//output:
[name2: Loy, name1: Roy]
我的问题是,
是否保证词典内容不改顺序不变?
例如上面的例子,无论运行多少次,第一次尝试总是returns相同的顺序?
不,不能保证。没有订单就意味着没有订单。不要试图依赖未记录的事故。事实上,Apple 明确警告说,在现代 Swift 中,顺序是随机的,并且对于不同的运行可能会有所不同。
在您的示例中,您只有一个文字,您可以通过转换为 KeyValuePairs.
来获得有保证的顺序只要不修改字典,key/value 对的顺序就不会改变。其他一切都未指定。来自 documentation(强调已添加):
The order of key-value pairs in a dictionary is stable between mutations but is otherwise unpredictable.
注:这仅适用于printing/enumerating单个程序中的字典运行。 As @ matt 说,每个节目的顺序是随机的 运行。这是因为 Hasher
是随机的:
Do not save or otherwise reuse hash values across executions of your program. Hasher is usually randomly seeded, which means it will return different values on every new execution of your program. The hash algorithm implemented by Hasher may itself change between any two versions of the standard library.
哈希随机化在 Swift 4.2 中强制实施,实施 SE 0206 Hashable Enhancements:
To implement nondeterminism, Hasher uses an internal seed value initialized by the runtime during process startup. The seed is usually produced by a random number generator, but this may be disabled by defining the SWIFT_DETERMINISTIC_HASHING environment variable with a value of 1 prior to starting a Swift process.