Hashable problems

I’m trying to use NavigationStack and it requires my class to be Hashable. However, I get this error when I add Hashable to the Class. Please help.

What code do you have in your View where you are using an array of elements of that class? Is it in a List() or a ForEach()?

You should make Patient into a struct, but also can you paste the entire model? Because according to the error, it’s an error with Equatable not Hashable

The error is because, as per the documentation: “The Hashable protocol inherits from the Equatable protocol, so you must also satisfy that protocol’s requirements.”

@mikaelacaron is right, though. You should make Patient a struct instead of a class, unless you have a very good reason to keep it a class. A side benefit is that if Patient is a struct then you can get automatic synthesis of Hashable conformance from the compiler (and Equatable will come along for the ride too), provided that HistoryAndPhysical also a) is a struct, and b) conforms to Hashable.

If, for some reason, you have to keep Patient as a class, then you will need to manually implement Equatable conformance by supplying a custom == method in Patient:

static func == (lhs: Patient, rhs: Patient) -> Bool {
    //determine whether lhs and rhs are equal
}

Thanks, I will work on it

Related to this issue, I have this error when I get to the NavigationLink within my NavigationStack.

Blockquote
@State private var path = NavigationPath()
var body: some View {

    NavigationStack(path: $path) {
        
        ScrollView {
            
            VStack (alignment: .leading) {
                ForEach(filteredPatients) { patient in
                    
                    
                    
                    VStack(alignment: .leading) {
                        NavigationLink(value: patient) {

Blockquote

I get this error on the last line: Initializer ‘init(value:label:)’ requires that ‘Patient’ conform to ‘Hashable’

So, this is my model"

Blockquote
import Foundation
import SwiftUI

class Patient: Identifiable, Decodable, ObservableObject {

// The id property is for the Identifiable protocol which we need to display these instances in a SwiftUI List
var id: UUID?

// These properties map to the properties in the JSON file
var name: String
var DOB: String
var CurrentPt: Bool
var Image: String
var Facility: String
var Diagnosis: String
var ROS: String
var PE: String
var MDM: String
var Assessment: String
var Plan: String
var CPT: String

//Trying to make Patient hashable
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
static func == (lhs: Patient, rhs: Patient) → Bool {
return lhs.id == rhs.id
}

Blockquote
I cannot add “Hashable” to the class “Patient” without numerous errors. This array is populated with a json file. Nor can I change the class to a struct. You can see where I tried the hash into trick but that doesn’t get rid of the error on the view.

Any ideas? Thanks, Todd

You do not conform to Hashable:

class Patient: Identifiable, Decodable, ObservableObject

No Hashable there.

That worked. but I know it wasn’t working before. Thanks

Note, I’m moving your question to app development, because that’s where it best fits, not community