中没有更多上下文的表达式类型不明确

中没有更多上下文的表达式类型不明确

本文介绍了在 Xcode 11 中没有更多上下文的表达式类型不明确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 @EnvironmentObject 中引用一个 [Item] 列表,但是在 SwiftUI 中访问它时列表,我收到错误.我不明白的是,跟随 Apple 的地标时不会弹出此错误教程.

据我所知,[Item] 列表正在正确加载,因为我可以将其打印出来并使用它执行其他功能.它只是在将它用于 SwiftUI List 时出错了,我错过了什么吗?

ItemHome.swift:

struct ItemHome : 查看 {@EnvironmentObject var dataBank:DataBankvar主体:一些视图{列表 {ForEach(dataBank.itemList) { item inText("\(item.name)")//表达式类型不明确,没有更多上下文}}}}

支持代码如下:

项目结构:

struct Item {变量 ID:整数变量 uid: 字符串var 公司:字符串var item_class: 字符串变量名:字符串var 股票:Intvar average_cost:十进制var otc_price:十进制var Dealer_price:十进制var ctc_price:十进制}

DataBank.swift:

final class DataBank : BindableObject {let didChange = PassthroughSubject()var itemList: [Item] = load("itemsResults.json") {已设置{didChange.send(self)}}}func load(_ filename: String, as type: T.Type = T.self) ->T{让数据:数据守卫让文件 = Bundle.main.url(forResource: 文件名, withExtension: nil)别的 {致命错误(在主包中找不到 \(文件名).")}做 {数据 = 尝试数据(内容:文件)} 抓住 {致命错误(无法从主包加载\(文件名):\n\(错误)")}做 {让解码器 = JSONDecoder()返回尝试decoder.decode(T.self, from: data)} 抓住 {fatalError("无法将 \(filename) 解析为 \(T.self):\n\(error)")}

}

itemsResults.json:

[{身份证":1,"uid": "a019bf6c-44a2-11e9-9121-4ccc6afe39a1","company": "生物种子","item_class": "种子","name": "9909",股票":0,平均成本":0.0,场外交易价格":0.0,经销商价格":0.0,ctc_price":0.0},{身份证":2,"uid": "a019bf71-44a2-11e9-9121-4ccc6afe39a1","公司": "先锋","item_class": "种子","name": "4124YR",股票":0,平均成本":0.0,场外交易价格":0.0,经销商价格":0.0,ctc_price":0.0}]
解决方案

显然我错过了确保我的模型(在本例中为Item)符合 Identifiable 协议的问题.它.不过,我希望 Apple 能更清楚地说明他们的错误消息.

I'm trying to refer to an [Item] list within an @EnvironmentObject however when accessing it within a SwiftUI List, I get the error. What I don't understand is, this error doesn't pop up when following Apple's Landmark tutorial.

As far as I can tell, the [Item] list is loading correctly as I can print it out and do other functions with it. It just bugs out when using it for a SwiftUI List Is there something I've missed?

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
            }
        }
    }
}

Supporting code below:

Item Struct:

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
    }
]
解决方案

Apparently I missed making sure my models (Item in this case) conformed to the Identifiable protocol fixed it. Still, I wish Apple was more clear with their error messages.

这篇关于在 Xcode 11 中没有更多上下文的表达式类型不明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 23:44