Learn Courses My Dashboard

Help Needed - Fatal error: Unexpectedly found nil while unwrapping an Optional value

Struggling to find out why the app is crashing. I’m working with an API that I know is working. I can see data coming back and I’ve checked it in Proxyman. For some reason when I try to check if the value is nil it still crashes even though I thought i’d protected the app from it with a ProgressView() until there’s data back from the API.

If you use Optional chaining in your test, and provide a default value in the event that the contents are nil, kind of like this:

if Double(model.coinPrices.prices.btc!.last ?? 0) != nil {

it will overcome the view seeing nil before the API actually returns data.

You may have to do this…

if Double(model.coinPrices.prices.btc?.last ?? 0) != nil {

(edit)
Force unwrapping a optional value is deadly if the value is nil as it will crash your App.

Hi Chris,

Firstly, thanks for your help mate…

I tried Optional Chaining… i think my code might be incorrect. Worth nothing that both ‘btc’ and ‘last’ are optionals and that the below reference should return a string value. The reason i convert it to a double is because xCode complains that the string will also return true when checked for being nil.

model.coinPrices.prices.btc!.last!

As such I tried using the below which still fails.

I think you have to use Optional chaining within the Double() parenthesis as that is the item that is nil.

Can you describe what you are trying to achieve. Maybe there is an alternative way of checking if there is data and if there are no records then show the ProgressView. Can you perhaps use something like:

if array.count != 0 {
   // Process records
} else {
    ProgressView()
}

Do you see what I am getting at?

Hey Chris,

Yeah I tried that also to no avail… I just got it to work and maybe you can explain why to me.

Where my cursor is next to the first ?.. when this is a ‘?’ the code works… When I change this to ‘!’ and force unwrap ‘btc’, it fails.

In short…the below code works.

OK that’s the answer. So btc is a String and ? means that it is an optional and could be nil so you are telling the complier that maybe the result could be nil and if so provide a default value which you have provided with ""

.last is a method on an array which I don’t think requires the force unwrap. Try removing the ! after .last and it should operate the same.

Hey Chris,
Sorry I see where we’re getting confused… ‘last’ is a variable within ‘btc’. This isn’t an array. For clarity, here is my model file. As you can see, both ‘btc’ and ‘last’ are optional.

Edit: This is this variable and instance i’m referencing: @Published var coinPrices = CoinPrice()

Oh right.

The other thing to remember is that when SwiftUI renders a View is creates and destroys a View multiple times and in the process that test is being performed many times even when there is no data and then there is data. I’m guessing that model is your ObservableObject and coinPrices is your @Published property so initially as the View is being rendered there is a likelihood that coinPrices is empty until the API call populates it.

Because everything occurs so fast it seems reasonable to assume that data is already there but that may not be the case so by using Optional chaining and providing a default value account for the instances that there is no data when the View is being created and destroyed.

Hope that makes sense.

Makes sense Chris. Appreciate the assistance. :smiley: