我正在创建 2 个包含 String
的集合,如下所示
let set1 : Set<String> = (["07:00", "QShift", "PRN"])
let set2 : Set<String> = (["07:15", "QShift", "PRN"])
起初,我认为每个集合中字符串的顺序将与它们添加时的顺序完全相同。但是我对 set1
中的一个是错误的,控制台给了我这个
Printing description of set1:
▿ 3 elements
- [0] : "QShift"
- [1] : "PRN"
- [2] : "07:00" { ... }
Printing description of set2:
▿ 3 elements
- [0] : "07:15"
- [1] : "QShift"
- [2] : "PRN" { ... }
有谁知道为什么 set1
中的字符串顺序已更改为 QShift, PRN, 07:00
?
最佳答案
因为Set
是一个无序的数据结构。
Set引用:
A set stores distinct values of the same type in a collection with no defined ordering. You can use a set instead of an array when the order of items is not important, or when you need to ensure that an item only appears once.
你应该看看NSOrderedSet类引用,更多 API:
NSOrderedSet and its subclass, NSMutableOrderedSet, declare the programmatic interfaces to an ordered collection of objects.
关于ios - 集合中项目的顺序已更改,Set,Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38556070/