25

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
  • Print what is displayed in console? It seems like there is maybe an index out of range error. Check the any arrays you may have at your destination view controller. Or just check that that ViewController you are navigating to with the Tab is initialized and in the tabController.viewControllers array.
    – Alexander
    Oct 9, 2019 at 12:54
  • Hi @Alexander, for the initialization, I'm sure my views are initialized properly. And yes, I'm also thinking it's an array problem, but I don't know which array in which function and in which class, because that view and its subviews are filled with arrays. That's why I want to know how to debug properly. Currently, I'm trying to change many things in my code, hoping I'd hit a fix (even unknowingly).
    – ygee
    Oct 10, 2019 at 9:23
  • I'm getting this, in a very similar situation. For me, the app crashes with the same error and debug console output. It appears to be a C++ precondition failure (so within Swift itself - hence by the breakpoint doesn't work). For me, it happens when trying to access the size of a GeometryProxy. Nov 13, 2019 at 17:48
  • Same here - accessing the size of GeometryProxy, to copy into a @Binding
    – Grimxn
    Nov 25, 2019 at 21:34
  • Happens to me to. Next call in the stack is GeometryProxy.size.getter() however it only occurs for me if I access SwiftUI tabs in a certain order. GeometryReader is only used in the tab that causes the crash, (Crash only occurs if that tab is accessed third).
    – tarasis
    Dec 17, 2019 at 16:08

8 Answers 8

22

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")
        }
5
  • I had same error with Form inside a TabView that was already inside a NavigationView. To work around the crash I had to wrap the Form in another dummy NavigationView and hide it off-screen with .padding(.top, -60). 🤷‍♂️
    – Palimondo
    Jan 9, 2020 at 16:25
  • 5
    this works, but it is not ideal. I don't want a navigation view.
    – imthath
    Feb 12, 2020 at 12:48
  • My Custom View had List in the NavigationView already. What I did is I removed the NavigationView from my Custom View and added it as you have specified above. After that change, the crash went away. Apr 1, 2020 at 19:54
  • @Palimondo I think it should be -100 and not -60. thanks for the fix
    – PaFi
    Apr 20, 2020 at 16:07
  • @Imthath I agree with you
    – Cagatay
    Apr 26, 2020 at 11:46
12

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.

2
  • Yeah, NavigationView was the issue also in my case
    – orkenstein
    Jul 29, 2020 at 15:31
  • I wish I could say the same, I put .navigationViewStyle(StackNavigationViewStyle()) on NavigationView but i am still encountering the error
    – Learn2Code
    Apr 15, 2021 at 21:46
5

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
}
1
  • I also had this crash because the navigationBarItems was inside a wrong block
    – jfredsilva
    Mar 5, 2021 at 11:55
0

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.

2
  • 1
    This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question: stackoverflow.com/questions/ask. You can also add a bounty to draw more attention to this question once you have enough reputation: stackoverflow.com/help/privileges/set-bounties
    – borchvm
    Sep 8, 2020 at 5:34
  • 4
    you are correct borchvm - yet humans appreciate validation and additional info - both included in my response.
    – David
    Sep 8, 2020 at 20:16
0

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.

0

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.

0

I had the same problem. I just removed the random id (UUID) from the Identifiable and I fixed my crash

0

In my case a "precondition failure" arose because I was requesting AXUI attributes from my application process on startup:

ForEach(
     NSWorkspace.shared.runningApplications, // Maybe this only works with `.filter(...)` 
     id: \.processIdentifier
) { runningApp in

    // if(runningApp != NSRunningApplication.current) { // This condition made it work for me

    var appAXUI: AXUIElement = AXUIElementCreateApplication(runningApp.processIdentifier)
    var element: AnyObject? = nil
    AXUIElementCopyAttributeValue(appAXUI, "someAttr" as CFString, &element)    
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.