I'm running into an issue wherein the pattern I am attempting to implement does not play nicely with SwiftUI statefulness and refuses to update structs that conform to my TestProtocol
when they are in an array.
I am chalking this up to my fundamental misunderstanding so would appreciate any guidance on the matter.
Here is a sanitized code snippet that outlines the goal I was driving for in the hopes that providing additional clarity might help in suggesting a possible solution.
protocol TestProtocol: Identifiable, Hashable, Equatable {
var id: UUID { get }
}
protocol ATestProtocol: TestProtocol {
var aValue: String { get set }
}
protocol BTestProtocol: TestProtocol {
var bValue: Bool { get set }
}
struct ATestStruct: ATestProtocol {
let id = UUID()
var aValue: String = ""
}
struct BTestStruct: BTestProtocol {
let id = UUID()
var bValue: Bool = false
}
struct TestStruct: Identifiable, Hashable, Equatable {
let id: UUID = UUID()
let testArray: [any TestProtocol]
public func hash(into hasher: inout Hasher) {
hasher.combine(id)
hasher.combine(testArray)
}
static func == (lhs: Self, rhs: Self) -> Bool {
lhs.id == rhs.id && lhs.testArray == rhs.testArray
}
}
class TestObservable: ObservableObject {
static let shared = TestObservable()
@Published var filters: [TestStruct]
init() {
self.filters = [
TestStruct(
testArray: [
ATestStruct(),
BTestStruct()
]
)
]
}
}