HomeiOS Developmentios - Array of protocols utilizing kind erasure can't conform to Hashable...

ios – Array of protocols utilizing kind erasure can’t conform to Hashable or Equatable protocols


I am operating into a problem whereby the sample I’m making an attempt to implement doesn’t play properly with SwiftUI statefulness and refuses to replace structs that conform to my TestProtocol when they’re in an array.

I’m chalking this as much as my basic misunderstanding so would recognize any steerage on the matter.

Here’s a sanitized code snippet

protocol TestProtocol: Identifiable, Hashable, Equatable  {
    var id: any Identifiable { get }
}

struct TestStruct: Identifiable, Hashable, Equatable {
    let id: UUID = UUID()

    let testArray: [any TestProtocol]

    public func hash(into hasher: inout Hasher) {
        hasher.mix(id)
        hasher.mix(testArray) /// ERROR: Sort 'any TestProtocol' can't conform to 'Hashable'
    }

    static func == (lhs: Self, rhs: Self) -> Bool {
        lhs.id == rhs.id && lhs.testArray == rhs.testArray /// ERROR: Sort 'any TestProtocol' can't conform to 'Equatable'
    }
}

class TestObservable: ObservableObject {
    static let shared = TestObservable()

    @Printed var filters: [TestStruct]

    init() {
        self.filters = [
            TestStruct(
                testArray: [
                    ...
                ]
            )
        ]
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments