2

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 Answer 1

5

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

Worked like a charm! Thanks! Could you explain what it exactly does?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.