How to Know Which Line of Code Has Caused My iOS App to Crash in Xcode 9

How do I know which line of code has caused my iOS app to crash in Xcode 9

What you need here is an exception breakpoint. An exception breakpoint is a special kind of breakpoint that breaks whenever an exception occurs, instead of on a specific line in a specific file every time. The line where the breakpoint breaks is the line that throws the exception. To set an exception breakpoint, you'll need to open the breakpoint navigator and press the + button in the bottom left

Xcode breakpoint navigator

A little menu will pop up, and from that, you select Exception Breakpoint…

Exception Breakpoint option

A 3rd menu might pop up afterwards, but you can just click anywhere outside of it to dismiss it. After that, when you build and run your app again, it will break whenever an exception is thrown on the line that it is thrown

Determine source code line No. from Swift Crash log

frame#3 tells me there's a closure in MemberAllSpottingSessionsViewModel.getSessionsForPageOne() that's calling swift_unexpectedError (ErrorType.swift, line 188)

i googled 'ErrorType.swift libswiftcore' and found https://github.com/apple/swift/blob/main/stdlib/public/core/ErrorType.swift#L188, which told me

'try!' expression unexpectedly raised an error: \(String(reflecting: error))

look for a try! and rewrite it without a !

Xcode doesn't show the line that causes a crash

You should also ensure that you have breakpoints set for all exceptions. This will cause Xcode to stop at the line where the exception is occurring. Do the following [in Xcode 4]:

  1. In the Project Navigator on the left side of Xcode, click on the breakpoint navigator (almost all the way to the right hand side of the top button bar. The icon looks like a fat right arrow).

  2. At the bottom of the navigator, click the "+" button.

  3. Click "Add Exception Breakpoint".

  4. A new breakpoint will be created. It should be configured as needed but you can tweak its behavior.

  5. Run your project and reproduce the exception.

Also you mentioned that you linked to some 3rd party libraries/frameworks. If the exception is occurring within those frameworks then you are going to have a hard time since the code is compiled and Xcode can't actually show you the line that caused the exception. If this is the case and you are certain you are using the libraries correctly, then you should file a bug report to the maintainers of those libraries.

How to find the exact exception that's being occured or the root cause from the crash logs ios

Unrelated, but in Swift, there is no need for ; at the end of the lines.

Let's read the crash log:

2   watch Extension                 0x04488074 closure #1 in OtpView.fetchProfile() + 1232 (OtpView.swift:203)
3 watch Extension 0x0443c47c specialized closure #1 in closure #1 in useInteceptor(urlString:method:requestBody:completionHandler:) + 1120 (Inteceptor.swift:0)
4 watch Extension 0x0443dc80 partial apply for specialized closure #1 in closure #1 in useInteceptor(urlString:method:requestBody:completionHandler:) + 72 (<compiler-generated>:0)

That's the interesting part (the top ones with names of methods), you read them from the bottom.

So in Interceptor.swift, method useInteceptor(urlString:method:requestBody:completionHandler:) is called at some point.

In this one, there is a closure (or multiple ones), but at least, in the first one (closure #1), which is completionHandler, that you call in fetchProfile of OptView class in OptView.swift file.

And there, in line 203, it's the line causing the crash.

Let's analyze the culprit:

let profile = try! JSONDecoder().decode(ProfileResponse.self, from: data)

Here, the possible crash is because of try!.

Why did you use try! instead of writing a do/try/catch? Do you know why you used a force unwrap ! here? What it means?

It means that if an error is thrown, just make the app crash.

So if there is a crash, it's expected behavior, since you explicitly wrote "crash here if there is thrown error".

So, now, why would decode(_:from:) crash?

I don't know what's ProfileResponse, but that method could throw an error because you didn't specify to it that a value in the JSON can be nul, a value in the JSON can be omitted, because there is another issue with the received JSON, or JSON is invalid.

Or, because your API is giving a bad value. It's sometimes the cases when API encounters an error, they could responds: {"error": "some reason why it failed"}. It's a valid JSON, but I don't think that ProfileResponse expect to be like that.

Now, as why it giving bad response, it's up to your Web API, check the doc, check the API developers for possible responses: Did you use bad parameters, are you falling into the one case not handled by back-end?

So when you wrote that line with the try!, you decided to tell: "Don't worry about the response, if there is a response, I'm sure of it that it can be decoded into a ProfileResponse object. Trust me, I know what I'm doing, I guarantee it will be always valid, and if that's not the case, just crash, that would prove me wrong, but rest assured, I'm sure of myself". Yes, that what meant try!.

If you don't want to crash, don't use !, and write a property do/try/catch.

do {
let profile = try JSONDecoder().decode(ProfileResponse.self, from: data)
if let ratePlanDetails = profile.response?.detail {
self.navigateToNext.toggle()
}
} catch {
print("Oops, there was an error while decoding ProfileResponse: \(error)")
print("And the API response was: \(String(data: data, encoding: .utf8) ?? "unknown data")")
}

Now, as to why you have an invalid response, that's up to your debugging: trying to reproduce it, with specific params, specific case, etc.). We can't guess what's wrong, we can't guess what's returning the API.
Once you know what's the real response sent back by your API, and don't know how to handle it, you can ask a new question on SO and we might help you, but at this point, we can't do anything more about your issue.

App Crashing Entire Device On Segue for iOS 9 + Xcode 7

I think I figured it out! I'm pretty sure my problem is the same as yours. Same crash log and situation.

I tried to isolate the problem, so I copied my storyboard into a blank project and removed all connections and made all classes default (no custom classes).

After some playing around, I decided to try to re-link a different split view controller to the same master and detail classes. It works! So I compared all of the settings and literally nothing was different. Damn.

What now? Open the source code. Right click your storyboard in the left pane and select "Open with external editor". This should open up the source code of the storyboard. I compared the source code of the two split view controllers and found a difference.

This is where I looked

<!--Split View Controller-->
<scene sceneID="X6N-vM-fHn">
<objects>
<splitViewController id="xSd-V6-k6W" customClass="SplitViewController" sceneMemberID="viewController">
<navigationItem key="navigationItem" id="yvV-sB-yKa"/>
<keyCommands>
<keyCommand/>
</keyCommands>
<connections>
<segue destination="PW6-z0-erU" kind="relationship" relationship="masterViewController" id="MBC-0A-hls"/>
<segue destination="xqk-PP-nzR" kind="relationship" relationship="detailViewController" id="sMq-cw-27p"/>
</connections>
</splitViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="nG8-BB-Qmu" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-157" y="-370"/>
</scene>

What's the difference?

<keyCommands>
<keyCommand/>
</keyCommands>

I don't know what it is, or how it got there, but when I removed those 3 lines the crashes went away. There is a UIKeyCommand class, but I never used it so I'm not sure if it's relevant. Hope this helps!



Related Topics



Leave a reply



Submit