I need some help trying to figure out what a mangled error message really means.

so... it's impractical and just a bit too much to post the code... it's an entire app, several frameworks, and well... a LOT of code. So I'm hoping for some more general advice on this one.


I am getting an error message of : Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

at a very wierd spot.

on the first case of a switch, where the item being switched is NOT an optional.

The property being switched is a String, and I can see it in the debugger: It's not optional and it != nil.


I figured I was facing a recursion issue, but I've learned how to recognise them in the debugger, and nope. Not recursion (no irrevocable loop here)


Here's what I most recently did to my app: I unembedded a View from a tabView in InterfaceBuilder, and had to re-connect 3 outlets.

the error I am getting is specific to making a selection of a model object. (which is the same thing as adding it to an array.)


so the error, is not telling me what's going wrong, and it's at a very wierd place that cannot possibly be the issue, and I am at a loss. there's no indication of _any_ optionals at all in this part of the app.

Replies

Does the error shows always at the same place ?


The problem with some errors is that they pop somewhere (I should say at some time) which is not the cause of the problem.


Could you try changing some timing (like going to release mode instead of debug, or adding sanitizers or other options, just to force changing timing).


If error occurs somewhere else, that will be an indication.


Otherwise, you need to dig into this part of code.


Then, could you tell a bit more about those strings that drive the switch ?

Could you try:

- printing all the strings before the switch

- copying in local variables and use them in switch.


That should be just to dig further the investigation.

When you get Fatal error: Unexpectedly found nil while unwrapping an Optional value, it is very sure that something is nil which should not be nil.


First, find the exact line which is causing the issue. That may happen where you have forced unwrapping, using variables declared as Implicitly Unwrapped Optionals, as well as using `as!` or `try!`. Try step by step execution or adding `print` every here and there.


Then, show all the relevant parts of your code.


Unfortunately, the debugger view of variables is not reliable enough and you should better not rely on it. And usually, recursive something has nothing to do with Unexpectedly found nil, so you may be goint into a wrong way.

the exact line where the crash occurs is in a switch case, where we are switching a non-optional String.

which is why I'm here. see above for a little more depth.

Does the error shows always at the same place ?


I would have said yes, until some changes I made.

I re-discovered a faulty assumption I was making... I have a heirarchy of objects managing completely unrealted objects, of the same class. And some of those objects were capable of wandering into the message chain of the hierarchy...


(this became clear once I started considering whether the error shows at the same place, thank you)


so I wrote an if statement that checks for membership in the hierarchy, and I thought I beat the problem. I tested it, and couldn't incur the error message at that same place. But I clicked elsewhere, and got the same error message, in a different switch statement, in a completely different part of the app, that does not have the same faulty assumption to fix.


so now my answer is: no?

looks like the error message is erroneous as well. It's probably triggering at the wrong time.

Did you try to look for zombies ?

Your added information gives me an impression that something very weird is happening, but it gives me no information how to solve it.


And switching a non-optional String does not mean there's no nil.

yeah,

I "think" i tracked it down. a lot of testing is called for to confirm.

the wierd part is the timing of the error. It's happening very late in the execution, making it seem like there's an optional value that is nil, where there shouldn't be. the train wreck starts much sooner, and should crash several calls earlier.


what really seems to be happening: I'm filtering an array that has been emptied, and I'm trusting the result to have a count of at least 1.


I tracked that back to when I fill the array, and that function seems to get called more than once (though, I haven't yet found out why that is.) So my code used to say: array = someOtherArray. But if you call that function twice, someOtherArray winds up being empty (because we actually move the items instead of copying them, so the second time, someOtherArray is empty) and someOtherArray winds up steamrolling the expected array.


so I put a conditional in that code that checks for array for a count, and if it has a count, add someOtherArray to it.

that seems to have fixed it. but we'll see.


thanks for the suggestions. I don't think I would have gotten this far without them.