【问题标题】:Use value of labels in array inside array and create forEach使用数组内数组中的标签值并创建 forEach
【发布时间】:2019-10-21 18:00:04
【问题描述】:

我有一个类似计划的 JSON

[
{
    "id": "registration",
    "title": "auto",
    "info": "under update",
    "brand": "TESLA",
    "model": "MODEL X",
    "make_year": 2017,
    "properties": [
        {
                "id": 1,
                "icon": "ic_rgb",
                "label": "color",
                "value": "red"
            },
            {
                "id": 2,
                "icon": "ic_car",
                "label": "tipe",
                "value": "car"
            },
            {   "id": 3,
                "icon": "ic_fuel",
                "label": "fuel",
                "value": "-"
            },

            ...

        ],
    },
{
    "id": "firstRegistration",
    "title": "auto",
    "info": "under update",
    "brand": "TESLA",
    "model": "MODEL X",
    "make_year": 2017
},
{
    "id": "secondRegistration",
    "title": "auto",
    "info": "under update",
    "brand": "TESLA",
    "model": "MODEL X",
    "make_year": 2017,
}
]

我的模型是:

...
    struct Unicards: Hashable, Codable, Identifiable {
    var id: String
    var title: String?
    var info: String?
    var brand: String?
    var model: String?
    var make_year: Int?
    var messege: String?
    var messege_color: String?
    var price: String?
    var price_currency: String?
    var price_tooltip: String?


    var properties: [HPropert]?
    struct HPropert: Hashable, Codable, Identifiable {

        var id: Int
        var icon: String?
        var label: String?
        var value: String?

    }
    }

这是我正在做的事情http://joxi.ru/Q2KLXvYHLDz9aA

我使用 ScrollView(Axis.Set.horizo​​ntal) 和 forEach 元素 但不能按 data.properties.count 循环元素

我已经解析 JSON,但无法按计数循环元素。

你能帮我举个例子吗?

我该如何尝试


var data2: Unicards.HPropert

        HStack {

            ForEach(data2) { item in <--\\Generic parameter 'Data' could not be inferred
                VStack {

                                Image(image)
                                    .resizable()
                                    .scaledToFit()
                                    .frame(width: 34, height: 34)


                                VStack {

                                    Text(self.data2.label ?? "")
                                        .font(.system(size: 14))
                                        .fontWeight(.medium)
                                    Text(self.data2.value ?? "")
                                        .font(.system(size: 14))
                                }


                }
            }
    }

【问题讨论】:

  • 请添加您用于循环元素的代码
  • 更新帖子。感谢关注
  • Identifiable 意味着编译器希望每个对象都有一个唯一的 id。 JSON 中的所有对象都具有相同的 id。
  • Thnx 我所有的对象现在都有唯一的 ID,我正在更新帖子,你能看看吗? Generic parameter 'Data' could not be inferred

标签: swift list foreach swiftui


【解决方案1】:

解决了

第一步是

var data2: Unicards  (your main data)

第 2 步 你体内的新常数

 var body: some View {
let arrayWhatYouNeed = data2.propertie 

别忘了返回你的视图

return  HStack {
...

现在你有例如

返回 HStack {

        ForEach(arrayWhatYouNeed) { item in 
            VStack {

                            Image(image)
                                .resizable()
                                .scaledToFit()
                                .frame(width: 34, height: 34)


                            VStack {

                                Text(item.lable)
                                    .font(.system(size: 14))
                                    .fontWeight(.medium)
                                Text(item.value)
                                    .font(.system(size: 14))
                            }


            }
        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    • 2019-09-20
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多