Binding with optional Value causes runtime crash

I have a binding with optional String as a type and in the parent view I have if condition which checks whether it is has value or not. Depending on this condition I show or hide the child view. When I make name value nil the app is crashing, below you find code example.

class Model: ObservableObject {

    @Published var name: String? = "name"
    
    func makeNameNil() {
        name = nil
    }
    
}

struct ParentView: View {
    
    @StateObject var viewModel = Model()
    
    var nameBinding: Binding<String?> {
        Binding {
            viewModel.name
        } set: { value in
            viewModel.name = value
        }
    }
    
    var body: some View {
        VStack(alignment: .leading, spacing: 8) {
            Text("Name is \(viewModel.name ?? "nil")")
            Button("Make name nil") {
                viewModel.makeNameNil()
            }
            if let name = Binding(nameBinding) {  /* looks like */
                ChildView(selectedName: name) /* this causes the crash*/
            }
        }
        .padding()
    }
}

struct ChildView: View {
    
    @Binding var selectedName: String
    
    var body: some View {
        VStack(alignment: .leading, spacing: 8) {
            Text("Selected name: \(selectedName)")
            HStack {
                Text("Edit:")
                TextField("TF", text: $selectedName)
            }
        }
    }
}

Here is stack of the crash.

Thread 1: EXC_BREAKPOINT (code=1, subcode=0x107e1745c)

AG::Graph::UpdateStack::update() ()
AG::Graph::update_attribute(AG::data::ptr<AG::Node>, unsigned int) ()
AG::Subgraph::update(unsigned int) ()

Looks like a switfui bug for me, should I avoid using such constructions?

The property is already a published entity in an observable object. Any changes to the name property will be propagated to the SwiftUI view. The issue was your nameBinding property. Unwrapping viewModel.name is you really had to do then passed the value into a Binding init constant as seen here ChildView(selectedName: .constant(name)).

struct ParentView: View {
    
    @StateObject var viewModel = Model()
    
    var body: some View {
        VStack(alignment: .leading, spacing: 8) {
            Text("Name is \(viewModel.name ?? "nil")")
            Button("Make name nil") {
                viewModel.makeNameNil()
            }
            if let name = viewModel.name {
                ChildView(selectedName: .constant(name)) 
            }
        }
        .padding()
    }
}

There is workaround how to avoid crash

var nameBinding: Binding<String>? {
    guard let name = viewModel.name else { return nil }
    return Binding {
        name
    } set: { value in
        viewModel.name = value
    }
}

...
   if let name = nameBinding {
       ChildView(selectedName: name)
   }
... 

Here is some other suggestions https://stackoverflow.com/questions/76241232/binding-with-optional-value-causes-runtime-crash/76243321#76243321

Binding with optional Value causes runtime crash
 
 
Q