Use*_*123 3 arrays dictionary loops swift
任何人都可以帮我把这个数组中的键值打印到控制台吗?
当我给出键名时,我能够遍历数组并打印出一个值.但我不知道如何循环键值aswel.
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"
}
Run Code Online (Sandbox Code Playgroud)
提前致谢
Cro*_*man 10
你可以这样做:
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)")
}
}
Run Code Online (Sandbox Code Playgroud)
哪个输出:
MiddleText: Lissabon
TimeToEndPoint: 10
TopTitle: Gate A1
BottomTextExtra: 15:00
BottomText: Check-in opens at
MiddleTextExtra: 15:15
Run Code Online (Sandbox Code Playgroud)