How to debug "precondition failure" in Xcode?

Viewed 10916

I'm building a SwiftUI App on Xcode 11 but is terminating immediately whenever I switch to a particular tab in the app.

Thing is, it always points to the Application Delegate file, which I think is not really the problem. I'm also getting this error in the console precondition failure: invalid input index: 2 and that's it, no more additional details on what file, array, or function this error is coming from.

enter image description here

Is there any way in Xcode to isolate which is causing this problem?

6 Answers

I had a TabView containing a view that used a List. When switching tabs, my app was crashing with a similar error: "precondition failure: attribute failed to set an initial value: 99" This crashed:

var body: some View {
    TabView {
        ListView()
        .tabItem {
            Image(systemName: "list.dash")
            Text("List")
        }

Wrapping the ListView in a NavigationView fixed the crash. I saw this use of NavigationView on "Swift Live – 007 SwiftUI TabView && List" by Caleb Wells. https://youtu.be/v1A1H1cQowI

https://github.com/calebrwells/A-Swiftly-Tilting-Planet/tree/master/2019/Live%20Streams/TabView%20List

This worked:

var body: some View {
    TabView {
        NavigationView { ListView() }
        .tabItem {
            Image(systemName: "list.dash")
            Text("List")
        }

I've run into this as well. I just want to share it in case someone finds it useful.

SHORT ANSWER

Wrapping my view into a NavigationView would raise the error. Using .navigationViewStyle(StackNavigationViewStyle()) solved my problem.

LONG ANSWER

I had something like this:

NavigationView {
    GeometryReader { proxy in
        VStack {
            Text("Dummy")
            Spacer()            
            MyView()    // CONTAINS HAS A GEOMETRY READER TOO
                .frame(width: min(proxy.size.width, proxy.size.height),
                       height: min(proxy.size.width, proxy.size.height)) 
            Spacer()
            Text("Dummy")
        }
    }
}

And then, MyView had a GeometryReader inside too. The code as described would fail. If NavigationView was removed, the precondition failure wouldn't happen.

I used .navigationViewStyle(StackNavigationViewStyle()) on NavigationView and that solved my issue.

I've had this runtime error on simulators. In my case the problem was NavigationBarItems, I used it inside a wrong block as below:

NavigationView {
    Group {
        if something {
            
            ScrollView {
                ...
            }//ScrollView
            
        } else {
            ...
        }
        
    }//group
        .navigationBarItems(trailing: self.favoriteItem) // CRASH**
}

I moved the NavigationBarItems modifier and gave it to ScrollView:

NavigationView {
        Group {
            if something {
            
            ScrollView {
                ...
            }//ScrollView
            .navigationBarItems(trailing: self.favoriteItem) // NO CRASH**
            
        } else {
            ...
        }
        
    }//group
}

I believe a bug has been filed with Apple https://developer.apple.com/forums/thread/133958 that concerns GeometryReader crashes in my case, I get a similar issue with a tabBar and the message "precondition failure: invalid input index: 2" when trying to use the GeometryReader as let w = geo.size.width

I've tried using a guard statement around it... didn't work - maybe I don't grok GUARD.

In my case I used a TabView which was already inside a NavigationView. In my 4th tab I used a Namespace for animation and after adding Namespace animation it was crashing. I tried removing GeometryReader from TabView, it worked but had other consequences. I added extra NavigationView in 4th tab, it also worked but also had other consequences. But the most appropriate approach for me was removing a Spacer() in the 4th tav. Inside that View where I added Namespace animation there was a Spacer() in the button which push all the view up, I removed that and everything is fine now.

Interesting I was using a NavigationView and changed it to a HSplitView and this crash started happening. As soon as I went back to the NavigationView this went away.

Related