Updating a State Variable When Another One Changes

The main ContentView of my app is an HStack that contains a List of Objects, a list of the Parts of the selected objects, and the details of the selected part:


Here's the ContentView code:


import SwiftUI

struct ContentView: View {
    var someObjects: [SomeObject]
    
    @State var selectedObject: SomeObject?
    
    @State var selectedPart: SomeObjectPart?
    
    var body: some View {
        HStack {
            SomeObjectView(someObjects: someObjects, selectedObject: $selectedObject).frame(width: CGFloat(exactly: 200))
            
            SomeObjectPartView(selectedObject: $selectedObject, selectedPart: $selectedPart).frame(width: CGFloat(exactly: 200))
            
            DetailsView(theDetails: selectedPart?.details ?? "").frame(width: CGFloat(exactly: 200))
        }
    }
}

I want to set selectedPart to nil when selectedObject changes, but I can't figure out how to do that. I tried to use didSet on selected object, but it doesn't seem to get called. I also tried setting it in SomeObjectView, but that generates a runtime warning.

Replies

Grrr... the screenshot didn't post. I'll try again:

OK, I give up. It won't take the screenshot. :-(