更新:添加关于 Hashable 的相同错误
我创建了一个Identifiable合规的协议和合规的结构。然后,当我创建列表并在 中引用它时ForEach,我收到错误(关于)Type 'any TestProtocol' cannot conform to 'Identifiable'我收到相同的错误。Hashable
我应该如何修复这个程序?
如果我写ForEach(list, id: \.id) ,它可以工作,但我认为遵守可识别性是没有意义的。
import SwiftUI
protocol TestProtocol: Identifiable, Hashable {
var id: UUID { get set }
var name: String { get set }
func greeting() -> String
static func == (lhs: Self, rhs: Self) -> Bool
}
extension TestProtocol {
static func == (lhs: Self, rhs: Self) -> Bool {
return lhs.id == rhs.id
}
}
struct Person: …Run Code Online (Sandbox Code Playgroud)