Xcode 11中没有更多上下文的情况下,表达式类型不明确。

13
我试图在@EnvironmentObject中引用 [Item] 列表,但是在SwiftUI List中访问时,会出现错误。我不明白的是,遵循苹果地标教程后,这个错误并没有弹出。就我所知,[Item]列表已正确加载,因为我可以打印它并进行其他操作,但当用于SwiftUI List 时,代码出现问题。我是否漏掉了什么?
ItemHome.swift:
struct ItemHome : View {

    @EnvironmentObject var dataBank: DataBank

    var body: some View {
        List {
            ForEach(dataBank.itemList) { item in
                Text("\(item.name)") // Type of expression is ambiguous without more context
            }
        }
    }
}

以下是支持代码:

物品结构体:

struct Item {

    var id: Int
    var uid: String
    var company: String
    var item_class: String
    var name: String
    var stock: Int
    var average_cost: Decimal
    var otc_price: Decimal
    var dealer_price: Decimal
    var ctc_price: Decimal
}

DataBank.swift:

final class DataBank : BindableObject {
    let didChange = PassthroughSubject<DataBank, Never>()

    var itemList: [Item] = load("itemsResults.json") {
        didSet {
            didChange.send(self)
        }
    }
}

func load<T: Decodable>(_ filename: String, as type: T.Type = T.self) -> T {
let data: Data

guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
    else {
        fatalError("Couldn't find \(filename) in main bundle.")
}

do {
    data = try Data(contentsOf: file)
} catch {
    fatalError("Couldn't load \(filename) from main bundle:\n\(error)")
}

do {
    let decoder = JSONDecoder()
    return try decoder.decode(T.self, from: data)
} catch {
    fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)")
}

}

itemsResults.json:

[
    {
        "id": 1,
        "uid": "a019bf6c-44a2-11e9-9121-4ccc6afe39a1",
        "company": "Bioseed",
        "item_class": "Seeds",
        "name": "9909",
        "stock": 0,
        "average_cost": 0.0,
        "otc_price": 0.0,
        "dealer_price": 0.0,
        "ctc_price": 0.0
    },
    {
        "id": 2,
        "uid": "a019bf71-44a2-11e9-9121-4ccc6afe39a1",
        "company": "Pioneer",
        "item_class": "Seeds",
        "name": "4124YR",
        "stock": 0,
        "average_cost": 0.0,
        "otc_price": 0.0,
        "dealer_price": 0.0,
        "ctc_price": 0.0
    }
]
5个回答

13

显然我没有注意确保我的模型(在这个例子中是 Item)符合 Identifiable 协议,现在已经修复了。但我仍希望苹果能够更清楚地显示错误信息。


6
正如您在答案中提到的那样,ForEach需要一个Identifiable对象列表。但是,如果您不想让您的对象实现该协议(或因某种原因无法实现),这里有一个小技巧:item.identifiedBy(\.self)

谢谢你的提示,piebie!对于Item来说,这很有效,因为它已经有了var id: Int,这自动使其符合Identifiable。不过我会记住这个方法,以防将来需要用到它! - Lorenzo Ang

2

我曾经也遇到过同样的问题,这并不是与代码本身有关的问题,而是与花括号/方括号有关。所以如果有人遇到同样的问题,但不知道具体出在哪里,可以尝试追踪一下花括号和方括号。


1

Xcode可能在许多情况下显示此错误。通常在使用高阶函数(如map)时,原因可能是“任何事情”。

这有两种类型:

  1. 错误出现在高阶函数中。在这种情况下,您唯一的选择是仔细阅读代码块。再次提醒,可能会有不同类型的问题。

  2. 在某些情况下,高阶函数本身没有任何问题,但在从高阶函数的主体调用的函数中可能存在编译时错误。 不幸的是,有时Xcode不指向这个错误。

    为了检测这种错误并节省大量时间,简单的解决方法是暂时注释高阶函数并尝试构建。现在Xcode将显示此错误函数,并显示更合理的错误。


1
为了符合Identifiable协议,只需给iduid变量赋一个唯一的值。

一种简单的方法是:

var uid = UUID()

所以你的完整结构体应该是:

struct Item: Identifiable {

    var id: Int
    var uid = UUID()
    var company: String
    var item_class: String
    var name: String
    var stock: Int
    var average_cost: Decimal
    var otc_price: Decimal
    var dealer_price: Decimal
    var ctc_price: Decimal
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接