arrays - Swift - 多维数组排序

标签 arrays swift tableview sections

为了以编程方式创建表格 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/

相关文章:

C++ 对数字字符串数组进行排序 - 比较函数不起作用

ios - 在 UITableView 中获取输入 onclick

swift - Switch 中 TableView 中的 UISwitch

ios - 如何在 ios 中向我的 ImageView 添加角标(Badge)?

ios - Swift Firebase 移除观察者不移除

ios - 将 [Int64] 的 Swift 数组转换为 NSArray

python - 类型错误 : Invalid dimensions for image data when plotting array with imshow()

javascript - 根据所选值显示数组中的值(从数组构建字符串)

javascript - 从当前月份开始对数组进行排序

ios - info[UIImagePickerController.InfoKey.mediaURL] 有时为 nil