SwiftUI-Array von Objekten mit AnyView
Sep 25 2020
Ich bin neu bei SwiftUI und probiere einige Dinge aus. Ich habe eine Liste, die aus einem Array erstellt wird. Ich möchte eine Navigation in Abhängigkeit von der angeklickten Zeile erstellen. Also habe ich eine Struktur mit folgendem erstellt:
struct DiscoverItem: Hashable, Equatable {
var name: String
var destination: AnyView
}
let arr = [
DiscoverItem(name: "Catalogus", destination: AnyView(ProductList(products: []))),
DiscoverItem(name: "Locations", destination: AnyView(LocationList()))
]
Allerdings sagt Xcode
Der Typ 'DiscoverItem' entspricht nicht dem Protokoll 'Equatable'.
Wie kann ich das lösen oder wie kann ich das richtig machen?
Antworten
1 Asperi Sep 25 2020 at 00:26
AnyViewDies liegt daran, dass die automatische Konformität mit diesen Protokollen deaktiviert wird (weil sie nicht konform sind).
Hier ist mögliche Lösung:
struct DiscoverItem: Hashable, Equatable {
static func == (lhs: DiscoverItem, rhs: DiscoverItem) -> Bool {
lhs.id == rhs.id
}
func hash(into hasher: inout Hasher) {
hasher.combine(id)
hasher.combine(name)
}
let id = UUID()
var name: String
var destination: AnyView
}