This suggests to me that my Sunday and Monday static instances are being passed by reference
No, that's not the problem.
But this way of adding a new trigger is causing the problem.
fetchTriggers.append(Int.random(in: 1..<3) == 1 ? .sunMorning : .monEvening)
This adds a new item. By it adds the same Sunday or Monday, with its existing ID.
So, you reuse the same struct.
Change to this to solve:
Button("New Trigger") {
let newTrigger = Int.random(in: 1..<3) == 1 ? FetchTrigger(dayOfWeek: .sunday, hour: 3) : FetchTrigger(dayOfWeek: .monday, hour: 6)
fetchTriggers.append(newTrigger)
// fetchTriggers.append(Int.random(in: 1..<3) == 1 ? .sunMorning : .monEvening)
}
You can also create a new ID:
struct FetchTrigger: Identifiable {
static var monEvening: FetchTrigger = .init(dayOfWeek: .monday, hour: 6)
static var sunMorning: FetchTrigger = .init(dayOfWeek: .sunday, hour: 3)
var id = UUID() // all need to be var
mutating func changedId() -> FetchTrigger {
self.id = UUID()
return self
}
and call:
fetchTriggers.append(Int.random(in: 1..<3) == 1 ? .sunMorning.changedId() : .monEvening.changedId())
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: