【问题标题】:Cannot convert value of type '[array]' to expected argument type 'Range<Int>'无法将类型“[array]”的值转换为预期的参数类型“Range<Int>”
【发布时间】:2020-02-18 21:46:24
【问题描述】:

我不明白可能是什么问题

我有一些 Json 例如是

[
{
    "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 

没有错误发生

【问题讨论】:

    标签: swift foreach swiftui


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      此错误表明您的data2.properties! 的类型不符合Identifiable 协议。 比如说,如果你的data2.properties!的类型是A,那么尝试这样做:

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

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

      【讨论】:

        【解决方案3】:

        尝试做这样的事情:

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

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

        【讨论】:

        • 你完全正确! ForEach(properties, id: \.self) ,它不起作用并且会犯更多错误,但是! Text(card?.label) 这正是我错过的,非常感谢
        猜你喜欢
        • 2020-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-12
        • 2023-03-24
        • 2016-08-01
        • 1970-01-01
        相关资源
        最近更新 更多