Is this a good idea? protocol IdentifiableHashable: Hashable, Identifiable { }

Identifiable has nothing to do with Hashable. It is there to expose a property which indicates whether 2 objects (which are not equal), are in fact 2 different versions of the same logical "thing".

So this:

Is the kind of thing you shouldn't do. What this is saying is that 2 Dossier objects with different images and names are interchangeable - that 2 Arrays which contain Dossiers with the same IDs are equal, even if each array contains entirely different versions of each Dossier.

let someDossiers: [Dossier] = [...]
var otherDossiers = someDossiers
for i in 0..<otherDossiers.count {
  otherDossiers[i].name = randomString()
  otherDossiers[i].image = randomImage()
}
someDossiers == otherDossiers // <- Do you expect this to return 'true'?

In the context of SwiftUI, you're telling the system that if it's already displaying the data from someDossiers, and you update it with a bunch a changes as above, that it doesn't need to refresh the display.

Same principle applies to Hashable.

8 Likes