在 Xcode 11 中没有更多上下文的表达式类型不明确
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/56683764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Type of expression is ambiguous without more context in Xcode 11
提问by Lorenzo Ang
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.
我试图在 a 中引用一个[Item]
列表,@EnvironmentObject
但是在 a 中访问它时SwiftUI
List
,出现错误。我不明白的是,在遵循Apple 的 Landmark 教程时不会弹出此错误。
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?
据我所知,该[Item]
列表正在正确加载,因为我可以将其打印出来并使用它执行其他功能。它只是在使用它时出错了SwiftUI
List
有什么我遗漏的吗?
ItemHome.swift:
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:
数据库.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:
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
}
]
回答by Lorenzo Ang
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.
显然我错过了确保我的模型(Item
在这种情况下)符合Identifiable
修复它的协议。不过,我希望 Apple 能够更清楚地了解他们的错误消息。
回答by piebie
As you mentioned in your answer, a ForEach
needs a list of Identifiable
objects. If you don't want to make your object implement that protocol (or can't for some reason), however, here's a trick:
正如您在回答中提到的, aForEach
需要一个Identifiable
对象列表。但是,如果您不想让您的对象实现该协议(或由于某种原因不能),那么这里有一个技巧:
item.identifiedBy(\.self)
item.identifiedBy(\.self)
回答by Akash Kundu
To conform to Identifiable
, just give the id
or uid
variable a unique value.
要符合Identifiable
,只需给id
或uid
变量一个唯一值。
An easy way to do this is this:
一个简单的方法是这样的:
var uid = UUID()
var uid = UUID()
So your full struct would be:
所以你的完整结构是:
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
}
回答by Omar HossamEldin
I had the same problem and it wasn't something related to the line itself, it was related to the curly braces/brackets, so that if someone faced the same problem and doesn't know where the problem is, try to trace the curly braces and the brackets
我遇到了同样的问题,它与线本身无关,它与花括号/方括号有关,因此如果有人遇到同样的问题并且不知道问题出在哪里,请尝试跟踪花括号大括号和括号