Thread 1 fatal error

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "ImageCell") as! ImageCell
    
    let resource = ImageResource(downloadURL: URL(string: arrayOfImages[indexPath.row])!, cacheKey: arrayOfImages[indexPath.row])
    
    cell.imgView.kf.setImage(with: resource)
    
    return cell
    
}

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

for the code:

let cell = tableView.dequeueReusableCell(withIdentifier: "ImageCell") as! ImageCell

is there any way I can fix this?

Thank you

Hi there,

This thread isn’t relevant to the Evolution discussion, but it would appear that there is either a) no cell dequeued from the reuse queue (as would happen when there’s nothing yet in the queue) or the dequeued cell is not of the type ImageCell.

From the context of your code, my guess is the former is true.

You should be careful to avoid force unwraps and force casts (the ! and as! operators) and instead use optionality to help you determine when things are nil or not what you’re attempting to cast them to.

Hello,

Yes I have figured this out and changed my code so the dequeued cell is of the type Image Cell and now I have run into this error

cell.imgView.kf.setImage(with: resource) Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

Again, something inside that group of elements, eg cell, imgView, kf, resource, or something inside how that method performs, is finding a nil value which is treated as not not nil, and the app is crashing when it tries to forcibly unwrap the optional, only to find there’s nothing inside the optional to unwrap.

From the looks of this, it appears something is Implicitly Unwrapped, so it’s optional, but the code tells the compiler that it should be treated as non-optional in most circumstances, which is why you may not need a ! operator in this code specifically.

I recommend you take a look through the Optionals section in the Swift Programming Language Guide.