How to debug "precondition failure" in Xcode? - Printable Version +- 0Day Forums (https://zeroday.vip) +-- Forum: Coding (https://zeroday.vip/Forum-Coding) +--- Forum: Swift (https://zeroday.vip/Forum-Swift) +--- Thread: How to debug "precondition failure" in Xcode? (/Thread-How-to-debug-quot-precondition-failure-quot-in-Xcode) |
How to debug "precondition failure" in Xcode? - leroyjeytyrupd - 07-18-2023 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][1]][1] Is there any way in Xcode to isolate which is causing this problem? [1]: RE: How to debug "precondition failure" in Xcode? - esperanzazy - 07-18-2023 In my case a "precondition failure" arose because I was requesting **AXUI** attributes from my application process on startup: ```swift 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) } ``` RE: How to debug "precondition failure" in Xcode? - harriettksm - 07-19-2023 I had the same problem. I just removed the random id (UUID) from the Identifiable and I fixed my crash RE: How to debug "precondition failure" in Xcode? - tallula899 - 07-19-2023 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. RE: How to debug "precondition failure" in Xcode? - flicked990383 - 07-19-2023 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. RE: How to debug "precondition failure" in Xcode? - thuyuxmfmy - 07-19-2023 I believe a bug has been filed with Apple [To see links please register here] that concerns GeometryReader crashesin 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. RE: How to debug "precondition failure" in Xcode? - palindromicallygshjrpo - 07-19-2023 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 } RE: How to debug "precondition failure" in Xcode? - apurveen - 07-19-2023 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. RE: How to debug "precondition failure" in Xcode? - tacho460 - 07-19-2023 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][1]. [To see links please register here] This worked: var body: some View { TabView { NavigationView { ListView() } .tabItem { Image(systemName: "list.dash") Text("List") } [1]: [To see links please register here] |