KitKit
KitKit

Reputation: 9523

How to use ForEach conform to protocol 'Hashable'

Hello I have an issue about confirm to protocol 'Hashable'. It's really suck. This is my model:

struct Page: Decodable, Identifiable {
    var id: String
    var name: String
    var thumbnail: String?
    var description: String
    var type: String
    var speechs: [String]
}

struct ExploreDataSource: Decodable, Hashable {   
    var title: String
    var data: [Page]
}

This is my ForEach code:

List {
    ForEach(self.VM.dataSource, id: \.self) { item in
        Text(item.title).bold().font(.system(size: 22.0))\
    }
}

Error:

Type 'ExploreDataSource' does not conform to protocol 'Equatable'. Do you want to add protocol stubs? Type 'ExploreDataSource' does not conform to protocol 'Hashable'

Upvotes: 11

Views: 12174

Answers (1)

grahan
grahan

Reputation: 2398

Your Page struct does not conform to Hashable respectively Equatable and therefore ExploreDataSource cannot conform to Hashable either.

So my suggestion is to make your Page conform to Hashable as well.

struct Page: Decodable, Identifiable, Hashable {
...
}

Upvotes: 14

Related Questions