Runtime error: precondition failure: attribute failed to set an initial value

Since it seems that this error - which can't be directly debugged - can be caused by so many different issues, I figured I'd throw my case up here too.

In my case, the error I was getting was:

precondition failure: attribute failed to set an initial value - 128

The issue was that I was attempting to present a sheet on a VStack that contained a NavigationView inside of it, like the below:

var body: some View {
    VStack(alignment: .center) {
        if /* some condition */ {
            /* some other content */
        } else {
            NavigationView {
                /* view content */
            }
        }
    }.sheet(isPresented: /* Binding value */) { 
        /* sheet content */
    }
}

The fix was to make sure that the sheet was being presented on the NavigationView instead:

var body: some View {
    NavigationView {
        VStack(alignment: .center) {
            if /* some condition */ {
                /* some other content */
            } else {
                /* view content */
            }
        }
    }.sheet(isPresented: /* Binding value */) { 
        /* sheet content */
    }
}

Seems obvious in hindsight, but it would have been nice to get a bit more information when the crash occurred in the first place.


For me it was displayMode inline in navigation bar title. Removing it fixes this problem.

Crash

.navigationBarTitle("Title", displayMode: .inline)

No crash

.navigationBarTitle("Title")

The problem is that your buttons are declared as computed property. To solve the crash declare them like this:

var buttons = [BugButtonBar.Info(title: "title", imageName: "text.insert"){}]

Turns out the id property of struct Info was the problem. Changed it to a computed property as follows:

var id : String {
   title + imageName
}

Great example of why I love/hate SwiftUI.