gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 11:24:55 25 4
gpt4 key购买 nike

我不明白可能是什么问题

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com