Fatal Error: Unexpectedly Found Nil While Implicitly Unwrapping an Optional vlaue

Hi everyone,

My code was running well yesterday, but when I ran it this morning I got the error:
Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
I have had this error before in the Advert class and was able to solve it by unwrapping the titleText. descriptionText and imagePath as shown (class on the right), but now when I run the app, I get the error at line 29 of AddListingTableViewCell class.

What would be the best way to solve this?

Any help would be great!

To avoid the error you can check to see if the value with there with a guard statement:

guard condition else {
    statements
}

Once you solved the crash problem with a guard statement, then set a break point and step through it to discover why the object or variable is empty.

Blessings,
—Mark

1 Like

Hi Mark,

Thanks for the reply! How would I implement the guard statement? Would it be something like:

var advert: Advert? {

    didSet {
        if let advert = advert {

           guard let logo = advert.imagePath, logo != "" else {
                print("image is nil")
          //      downloadImage(from: advert.imagePath)
                return
            }
                titleText.text = advert.titleText
                descriptionText.text = advert.descriptionText
                    }
                }
            }

This gets rid of the error, however now there is no way for the image to download from the image path so I think I’m still doing something wrong?
I have commented out the line the download image line as this seems to be what is causing the issue, but it’s the line I need to get working.

I’m very new to swift so your help is really appreciated :slight_smile:

Thanks,
Rebekah

Hi Rebekah,

Since Xcode is reporting nil, I would check the imagePath as it sounds like Xcode can not find it. Step through the method that downloads the image.

One possibility is that you are trying to DL from an unsecured site, http rather than https. Hard to tell with out seeing the code.

As I said, step through it until the code returns nil and then look into that.

Not very help I know. Sorry, Post the downloadImage code and we may have more suggestions.

Blessings,
—Mark

1 Like

Thanks Mark! I got it sorted in the end - I had the guard statement in the wrong place, but once I sorted that it got rid of the error and is running as expected again.
Thanks for your help.