I’m investigating real user issues through Crashes report in Xcode Organizer.
Xcode tells the error line but don’t give any information which value exactly caused the exception. There are many candidates to investigate.
I think it is not difficult work for Xcode to let me know which value caused the exception.
So.. I wonder is it possible to know which value exactly caused the nil exception from the crashes report?
2
Answers
I think best approach and main approach for hunting the
nil
value is :breakpoints in debug mode. So you can put breakpoints your specific lines and catch the
nil
value just before.Instead of blaming Xcode, write better code. Your exclamation points are the possible sites of these crashes. So eliminate them. There should be no exclamation points in your code. They mean "please crash me", so you can hardly complain when the runtime does just what you asked it to do.
For instance, as an example, where you say
standardDate!
, instead, ask yourself: ifstandardDate
isnil
, we will crash, so what would be an acceptable substitute in that case?If your answer is that there is no acceptable substitute, as it should never be
nil
, then the crash is good, as you are being alerted to a bug elsewhere in the program. Go off and figure out what that bug is.If there is an acceptable substitute in that case, such as the empty string, then use it here, e.g.
standardDate ?? ""
. In this way, eliminate all uses of exclamation points in your app.There are a few well-documented situations where an exclamation point is pretty much inevitable and cannot be eliminated, such as an
@IBOutlet
declaration or a cell subclass downcast, but otherwise there should be no exclamation marks in your code.