【问题标题】:How to get key and value from dictionary of objects in Swift?如何从 Swift 中的对象字典中获取键和值?
【发布时间】:2016-07-22 06:46:42
【问题描述】:

谁能帮我把这个数组中的键值打印到控制台吗?

当我给出键名时,我能够遍历数组并打印出一个值。但我不知道如何循环遍历键值。

var tableDataGates : [[String:String]] = [
      ["TopTitle" : "Gate A1", "MiddleText" : "Lissabon", "MiddleTextExtra" : "15:15","BottomText" : "Check-in opens at", "BottomTextExtra" : "15:00", "TimeToEndPoint" : "10"]
]

for x in 0 ..< tableDataGates.count {
    print(tableDataGates[x]["TopTitle"]); //PRINTS "GATE A1"
}

提前致谢

【问题讨论】:

    标签: arrays swift loops dictionary


    【解决方案1】:

    你可以这样做:

    var tableDataGates : [[String:String]] = [
        ["TopTitle" : "Gate A1", "MiddleText" : "Lissabon", "MiddleTextExtra" : "15:15","BottomText" : "Check-in opens at", "BottomTextExtra" : "15:00", "TimeToEndPoint" : "10"]
    ]
    
    for gate in tableDataGates {
        for (key, value) in gate {
            print("\(key): \(value)")
        }
    }
    

    哪个输出:

    MiddleText: Lissabon
    TimeToEndPoint: 10
    TopTitle: Gate A1
    BottomTextExtra: 15:00
    BottomText: Check-in opens at
    MiddleTextExtra: 15:15
    

    【讨论】:

      猜你喜欢
      • 2014-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-27
      • 1970-01-01
      • 2018-03-24
      • 1970-01-01
      相关资源
      最近更新 更多