SwiftUI array of objects with AnyView

Viewed 583

I'm new to SwiftUI and im trying some things out. I have a List which is being build out of an Array. I want to create a Navigation depending on the row that is clicked. So I've build a struct with the following:

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()))
]

However Xcode is saying

Type 'DiscoverItem' does not conform to protocol 'Equatable'

How can I solve this or which way is the proper way to do this?

1 Answers

It is due to AnyView which disables automatic conforming to those protocols (because does not conform to them).

Here is possible solution:

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