Is there a way to iterate through a Dictionary
in a ForEach
loop? Xcode says
Generic struct \'ForEach\' requires that \'[Str
Since it's unordered, the only way is to put it into an array, which is pretty simple. But the order of the array will vary.
struct Test : View {
let dict: [String: Int] = ["test1": 1, "test2": 2, "test3": 3]
var body: some View {
let keys = dict.map{$0.key}
let values = dict.map {$0.value}
return List {
ForEach(keys.indices) {index in
HStack {
Text(keys[index])
Text("\(values[index])")
}
}
}
}
}
#if DEBUG
struct Test_Previews : PreviewProvider {
static var previews: some View {
Test()
}
}
#endif