How to debug “precondition failure” in Xcode?

我只是一个虾纸丫 提交于 2020-06-24 08:43:21

问题


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.

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


回答1:


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")
        }



回答2:


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.



来源:https://stackoverflow.com/questions/58304009/how-to-debug-precondition-failure-in-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!