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

标签 swift foreach swiftui

我不明白可能是什么问题

我有一些 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 

没有错误发生

最佳答案

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

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

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

关于swift - 无法将类型 '[array]' 的值转换为预期的参数类型 'Range<Int>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58509407/

相关文章:

c++ - 为什么 foreach 使用 const 引用进行迭代?

SwiftUI,使用 ForEach 的参数导致错误 "Unable to infer closure type"

ios - 将子图层的路径更改为另一路径时创建动画

ios - swift/iOS 8 : search bar raising fatal error: unexpectedly found nil while unwrapping an Optional value

swift - 获取分段上传 Alamofire5 的上传进度

c# - C# Parallel.ForEach 是否对集合的迭代使用相同的线程

android - 循环通过 Radiogroup 以对 Radiobutton 标签执行操作

ios - 在 SwiftUI 中为屏幕上和屏幕外的文本添加动画效果

swift - SwiftUI 列表禁用滚动

ios - Segue 不适用于 Swift