![](/img/trans.png)
[英]How to iterate through a dictionary, loading the next item with button press in swift
[英]How to iterate through a dictionary in swift
當我的結構被調用/引用時,國家名稱被傳遞給它(即“英國”)。 然后,我想在“項目”字典中搜索該國家/地區的所有項目,並在導航列表中僅顯示每個項目的描述屬性。
我真的不明白問題是什么,更不用說解決方案了。
以下是錯誤:
這是我單擊第一個錯誤時的擴展錯誤:
這是整個代碼:
import SwiftUI
struct CountryView: View {
var country: String // Country name is passed in when struct called
let items = [
"United Kingdom": [
[
"description":"Diamond-Encrusted Ring",
"image":"placeholder",
"category":"jewellery",
"location":"Tate Modern Museum"
],
[
"description":"Red Baseball Cap",
"image":"placeholder",
"category":"clothing",
"location":"Old Trafford Stadium"
]
],
"United States": [
[
"description":"Apple Watch",
"image":"placeholder",
"category":"device",
"location":"Empire State Building"
]
]
]
var body: some View {
NavigationView {
List {
ForEach(items, id: \.self) { item in
NavigationLink(destination: ItemView(item: item)) {
Text(item.description)
}
}
}.navigationTitle(country)
}
}
}
struct CountryView_Previews: PreviewProvider {
static var previews: some View {
CountryView(country: "United Kingdom")
}
}
我知道這個問題令人困惑,因此請隨時對其進行編輯以使其更清晰。 我只是想不出更好的方法來解釋它,因為我不知道發生了什么。 提前致謝。
根據評論者的建議,我改用 model 而不是 dict。 它有效,雖然我不確定它應該是一個解決方案,更多只是一個小費。 它解決了這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.