Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug "precondition failure" in Xcode?

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?

like image 990
grey Avatar asked Oct 09 '19 12:10

grey


3 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")
        }
like image 186
beepscore Avatar answered Nov 03 '22 20:11

beepscore


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.

like image 36
Fernando Avatar answered Nov 03 '22 18:11

Fernando


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
}
like image 5
FRIDDAY Avatar answered Nov 03 '22 18:11

FRIDDAY