无法将类型“ [array]”的值转换为预期的参数类型“ Range <Int>”

时间:2019-10-22 17:21:10

标签: swift foreach swiftui

我不明白可能是什么问题

我有一些杰森 例如

[
{
    "id": 1,
    "title": "Title",
    "info": "info",
    "brand": "brand",
    "model": "MODEL",
    "make_year": year,
    "properties": [
        {
                "id": 1,
                "icon": "ic_rgb",
                "label": "color",
                "value": "red"
            },
            {
                "id": 2,
                "icon": "ic_car",
                "label": "type",
                "value": "value"
            },
            {   "id": 3,
                "icon": "ic_fuel",
                "label": "fuel",
                "value": "gas"
            }  
        ],
    },
{
    "id": 2,
    "title": "title2",
    "message": "massage2",
    "info": "wow you are amazing, thanks for the help",
    "properties": [
        {
                "id": 11,
                "icon": "ic_rgb",
                "label": "2color",
                "value": "2blue"
            },
            {
                "id": 21,
                "icon": "ic_car",
                "label": "2type",
                "value": "2cgh"
            },
            {   "id": 31,
                "icon": "ic_fuel",
                "label": "fuel",
                "value": "test"
            },
            ....
        ],
}
]

和我的模型

struct Unicards: Hashable, Codable, Identifiable {
var id: Int
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

}

还有什么问题...我正在尝试为属性的水平收集创建forEach方法

我部分成功,我使用嵌套数组实现了此方法,甚至可以正确生成单元数。哇!)

但是当我想通过索引为文本分配值时,会出现错误...

我实际上无法理解问题,因为已经通过单元格对象的数量计算并创建了数组。

但是当我要求公式给值赋值时

Cannot convert value of type '[Unicards.HPropert]' to expected argument type 'Range<Int>'

我的下面的代码

struct Card_cell_rowOfCheracteristics: View {

var data2: Unicards

var body: some View {
    let properties = data2.properties!

    return    VStack() {
//            Text("thanx bro!. have a nice day")
//                .font(.system(size: 14))
//                .font(.headline)
//                .fontWeight(.light)
//                .multilineTextAlignment(.leading)

        ScrollView(Axis.Set.horizontal) {



            HStack {


                ForEach(properties) { card in <--- Cannot convert value of type '[Unicards.HPropert]' to expected argument type 'Range<Int>'
                    VStack {
                        Image()
                            .resizable()
                            .scaledToFit()
                            .frame(width: 34, height: 34)


                        VStack {

                            Text(properties[card].label) 
                                .font(.system(size: 14))
                                .fontWeight(.medium)
                            Text(properties[card].value)
                                .font(.system(size: 14))
                        }


                    }
                }
            }
        }

    }
}

struct Card_cell_rowOfCheracteristics_Previews: PreviewProvider {
    static var previews: some View {
        Card_cell_rowOfCheracteristics(data2: PlatesData[0])
    }
}
}

但如果我说

properties[0].label 

没有错误发生

3 个答案:

答案 0 :(得分:12)

此错误表示您的data2.properties!的类型不符合Identifiable协议。 假设您的data2.properties!的类型为A,请尝试执行以下操作:

struct A: Identifiable {
    ...
    var id: String {
        return /*some ID*/
    }
    ...
}

这应该可以解决您的“问题”。

答案 1 :(得分:2)

尝试做这样的事情:

ForEach(properties) { card in
    .
    Text(card?.label) 
    .
}

我认为问题在于,当您遍历数组时,数组中的每个元素都称为card。因此,要获取数组中该元素的属性,您可以通过以下方法实现:card?.[property_name]

答案 2 :(得分:1)

正如@UndergroundFox指出的那样,解决此问题的一种方法是遵守Identifiable协议。另一个是在ForEach中指定一个键作为唯一标识符,在我的情况下,我使用了一个我知道是唯一的日期(因为我每天只有一个条目):

ForEach(arPictures,  id: \.date) { picture in