为了以编程方式创建表格 View ,我有一个如下所示的菜单设置,它包含部分和单元格。
var menu = [
"section 1": [
"point a",
"point b",
"point c"
],
"section 2": [
"point a",
"point b",
"point c",
"point d",
"point e"
],
"section 3": [
"point a"
]
]
struct Sections {
var sectionName : String!
var sectionObjects : [String]!
}
var sections = [Sections]()
for (key, value) in menu {
print("\(key) -> \(value)")
sections.append(Sections(sectionName: key, sectionObjects: value))
}
所以我希望它完全按此顺序插入(第 1 部分 -> 第 2 部分 -> 第 3 部分)。
但它实际上是按其他顺序插入的(来自打印):
section 2 -> ["point a", "point b", "point c", "point d", "point e"]
section 1 -> ["point a", "point b", "point c"]
section 3 -> ["point a"]
我完全不知道为什么菜单数组中的顺序会改变。 任何人都可以想象为什么或有一些建议吗?
谢谢和问候!
最佳答案
Swift 中的字典没有顺序。因此,当您遍历其中一个时,您不一定会按照添加它们的顺序遇到它们。
查看此答案了解更多详情:https://stackoverflow.com/a/24111700/3517395
关于arrays - Swift - 多维数组排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35526711/