【发布时间】:2019-12-29 16:38:50
【问题描述】:
我有一个符合Identifiable 协议的Message 数组,但我不断收到此错误:Generic parameter 'ID' could not be inferred。即使使用id: \.self 也不起作用。
这是怎么回事?
struct Message: Identifiable {
var id = UUID()
var text: String
var createdAt: Date = Date()
var senderId: String
init(dictionary: [String: Any]) {
self.text = dictionary["text"] as? String ?? ""
self.senderId = dictionary["senderId"] as? String ?? ""
}
}
@State var messages: [Message] = []
ForEach(messages) { message in
// Generic parameter 'ID' could not be inferred
}
【问题讨论】:
-
ForEach不能为空,必须有视图。 -
我已将您的 ForEach 放入 List 中,并且编译良好。使用 Xcode 11.2 测试。编译器可能会在 SwiftUI 中给出错误的位置和/或错误,因此您需要提供在哪里看到此错误的视图主体的完整代码快照。我想原因在不同的地方。