我正在從服務器獲取資料,我需要像在此站點SwiftUI 中顯示的那樣顯示它 - 嵌套串列
來自服務器的資料是
"OptionsListByItemId": [
{
"Choices": [
{
"ChoiceId": 1284226,
"ChoiceName": "Hot",
},
{
"ChoiceId": 1284227,
"ChoiceName": "Cool",
}
],
"OptionId": 187472,
"OptionName": "Temperature"
},
{
"Choices": [
{
"ChoiceId": 1284223,
"ChoiceName": "61%",
},
{
"ChoiceId": 1284224,
"ChoiceName": "70%",
}
],
"OptionId": 187473,
"OptionName": "Humidity"
]
}
我的模型就像
struct OptionsandChoices : Decodable , Identifiable{
var id: String{OptionName}
var OptionName: String!
var OptionId: Int
var Choices : [ChoiseList]
}
struct OptionsandChoiceList: Decodable{
var OptionsListByItemId:[OptionsandChoices]
}
struct ChoiseList: Decodable {
var ChoiceName: String!
var ChoiceId: Int
}
視圖模型是
class ItemChoiceViewModel : ObservableObject{
@Published var OpnChoice: OptionsandChoiceList = OptionsandChoiceList(OptionsListByItemId: [])
// fetching data from server
}
我的 swiftUI 視圖就像
struct ItemView: View {
var OpnChoice = OptionsandChoiceList(OptionsListByItemId: [])
@ObservedObject var choicevwModel = ChoiceViewModel()
struct Optionspage: View {
var body: some View {
List(choicevwModel.OpnChoice.OptionsListByItemId) {opn in
Text(opn.OptionName)
}
}
我無法在串列中使用 ChoiceName
我怎樣才能像我給的鏈接一樣在 OptionName 下方的每一行中獲得choiceName
串列應該顯示為
Temperature
Hot
Cold
Humidity
61%
70%
目前我有兩排
Temperature
Humidity
uj5u.com熱心網友回復:
您正在尋找的物業位于Choices。要獲得它的價值,請寫入以下內容:
opn.Choices[yourIndex].OptionName
此外,如果您想使用@ObservedObjectmake 您的OptionsandChoiceList類而不是 struct 并使其符合ObservableObject協議。最后,@Published在那里申報財產。
final class OptionsandChoiceList: ObservableObject, Decodable {
@Published var OptionsListByItemId: OptionsandChoices = []
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/315661.html
