我有以下可观的班子,我曾试图把它当作一个可观察到的物体,以及一种障碍和其他各种企图。 该物体由<代码>Locationservice类别保存,该类别也是。 ObservableObject
and it s being published in an [Location Toggleable]
. 我的理解是,对该阵列的一个单独部分进行更新,并不一定公布该元素的更新,因为阵列本身实际上没有更新,只是阵列的一个要素的财产。 在变更财产时,我如何适当更新在<条码>@Published条码>上的清单?
Failed Attempts
class LocationToggleable: Hashable, Identifiable {
// Omitted unrelated properties
var selected: Bool = false
}
class LocationToggleable: ObservableObject, Hashable, Identifiable {
// Omitted unrelated properties
@Published var selected: Bool = false
}
struct LocationToggleable: Hashable, Identifiable {
// Omitted unrelated properties
var selected: Bool = false
}
Location Service
class LocationService: ObservableObject {
//Omitted unrelated properties/functions
@Published var toggleableLocations: [LocationToggleable]
}
class LocationService: ObservableObject {
//Omitted unrelated properties/functions
@StateObject var toggleableLocations: [LocationToggleable]
}
class LocationService: ObservableObject {
//Omitted unrelated properties/functions
@ObservedObject var toggleableLocations: [LocationToggleable]
}
List
- Partial Code, the else is just a copy of the true return
ForEach(locationService.toggleableLocations, id: .self) { item in
if item.selected {
Section(content: {}, header: {
TrailDetailCardView(location: item.location)
.frame(maxWidth: .infinity)
.matchedGeometryEffect(id: "location-id-(item.id)", in: namespace)
.onTapGesture {
withAnimation {
proxy.scrollTo(item.id, anchor: UnitPoint(x: 0, y: 0.01))
locationService.setSelected(for: item, to: !item.selected)
}
}
})
.id(item.id)
}
Set Selected Function
- This works right now, but that s only because I m forcing a publish update for the
toggleableLocations
and it reeks of a code-smell.
func setSelected(for toggleableLocation: LocationToggleable, to selected: Bool) {
if let currentIndex = toggleableLocations.firstIndex(where: { $0.selected }) {
toggleableLocations[currentIndex].selected = false
}
toggleableLocation.selected = selected
toggleableLocations = toggleableLocations
}
Notes
- SwiftUI updating array state doesn t update view This solution doesn t work in my case, because I need the object to maintain it s
selected
state for proper management of it s layout in theLazyVGrid
. I also need the object to beHashable
for uses elsewhere in the code.