gpt4 book ai didi

ios - 集合中项目的顺序已更改,Set,Swift

转载 作者:可可西里 更新时间:2023-10-31 23:58:56 26 4
gpt4 key购买 nike

我正在创建 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com