Xcode not showing compile errors when app crashes

Hi Team,

I am using Xcode 15. When my app crashes, Xcode does not show at which point of the code the app crashes when in preview mode using SwiftUI.

Is there a setting I need to enable in order to show the position of compile error in the code when the app crashes?

Breakpoints are not working as well.

Thanks!

show the position of compile error in the code when the app crashes

Do you look for compile error or runtime crash ?

Could you show the code where you put breakpoint and explain why you think it should break there ?

That's a frequent problem with SwiftUI. Crash or compile error are not reported at the origin of the problem but later when system cannot cope with it.

You have a major misunderstanding about compilation errors, if you had a compilation error then your app would not even run. A compilation error means there is a syntactical error in your code, and it is nothing to do with a run time error.

You probably have an exception, in which case add code like this at the start of your app and then add a breakpoint to the logging line. The breakpoint will be hit when you run the app if that is the problem, then you can see the call stack.

func registerExceptionHandler() { NSSetUncaughtExceptionHandler { (exception) in NSLog("‼️‼️ Uncaught exception (String(describing: exception.reason))") } }

You can also extract a crash stack from the phone using Xcode

Thanks both for your suggestions. Apologies as I am new to Xcode. I meant Xcode could not show error of runtime crashes.

Here is where I suspect the runtime crash happening in ContentView:

...
var body: some View {
        NavigationView {
            List {
                ForEach(selectedContactIdentifiers.indices, id: \.self) { index in
                    Button(action: {
                        contactToEditIndex = index
                        showingActionSheet = true
                    }) {
                        HStack {
                            Image(systemName: "person.crop.circle.fill")
                                .foregroundColor(.gray)
                                .font(.largeTitle)
                            if let contact = fetchContact(with: selectedContactIdentifiers[index]){ //where runtime crash most probably happened
                                Text("\(contact.givenName) \(contact.familyName)")
                            } else {
                                Text("Unknown Contact")
                            }

...

}

func fetchContact(with identifier: String) -> CNContact? {
     let store = CNContactStore()
     let keysToFetch = [CNContactGivenNameKey, CNContactFamilyNameKey] as [CNKeyDescriptor]
     do {
         let contact = try store.unifiedContact(withIdentifier: identifier, keysToFetch: keysToFetch)
         return contact
     } catch {
         print("Error fetching contact:", error)
         return nil
     }
 }

...

I would have thought Xcode would automatically catch the exception and show the point of crash in the code.

As an alternative to mungbeans’ advice, try running your app outside of Xcode. Does it crash? If so, that should generate a crash report. If you post that here, we might be able to offer some advice on how to proceed.

See Posting a Crash Report for advice on how to post a crash report.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Xcode not showing compile errors when app crashes
 
 
Q