我想知道如何从Dictionary中打印键和值。
我的分组字典:
var Grouped: [String? : [(key: String, value: String?)]]
现在如何打印。
("9:00 - 22:00", [(key: "II", value: Optional("9:00 - 22:00")), (key: "III", value: Optional("9:00 - 22:00")), (key: "IV", value: Optional("9:00 - 22:00"))])
("7:00 - 22:00", [(key: "I", value: Optional("7:00 - 22:00"))])
("Closed", [(key: "VII", value: Optional("Closed"))])
("9:00 - 21:00", [(key: "V", value: Optional("9:00 - 21:00"))])
("10:00 - 20:00", [(key: "VI", value: Optional("10:00 - 20:00"))])
是否可以打印这样的值:
"9:00 - 22:00" - "II", "III", "IV"
"7:00 - 22:00" - "I"
"Closed" - "VII"
"9:00 - 21:00" - "V"
"10:00 - 20:00" - "VI"
在Swift中实现此目标的最佳方法是什么?
答案 0 :(得分:1)
要使用引号进行打印:
for case let key? in grouped.keys {
let values = grouped[key]!.map { "\"" + $0.key + "\""}.joined(separator: ", ")
print("\"" + key + "\"", "-", values)
}
示例输出:
"10:00 - 20:00" - "VI" "9:00 - 22:00" - "II", "III", "IV" "7:00 - 22:00" - "I" "Closed" - "VII" "9:00 - 21:00" - "V"
请记住,字典是无序的集合,因此每次打印行的顺序可能都不相同