How to debug "precondition failure" in Xcode?
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
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}