我想根据选择器选择过滤数据数组。我的问题是如何根据任何选定的国家填充/过滤城市阵列。示例代码如下:
static var countries: [String: [WorldData]] {
Dictionary(
grouping: worldData,
by: { $0.country }
)
}
static var city: [String: [WorldData]] {
Dictionary(
grouping: worldData,
by: { $0.city }
)
}
Picker(selection: $Country,
label: Text("Country")) {
ForEach(self.countries.keys.sorted(), id: \.self) { key in
Text(key).tag(key)
}
}
答案 0 :(得分:0)
睡个好觉后,我解决了自己的问题。如果有人需要它,这是解决方案:
static func filteredCities(key:String) -> [String: [WorldData]] {
var cities: [String: [WorldData]] {
Dictionary(
grouping: worldData.filter{$0.species == key},
by: { $0.city }
)
}
return cities
}