-1

I included all of the code below.

However...

The cell has two images, a background image (which is blurred) and another image ontop of that which is not blurred. When a user scrolls really fast in the tableview, I get the following error:

Unexpectedly found nil while unwrapping an Optional value: file csnios/CellEbayItems.swift, line 135

However, I have code that checks for it. It's also in a do/catch, but what can I do to fix this error?

if (self.imageBlur.image! != nil) {
    self.blurEffect()
}

class CellEbayItems: UITableViewCell {

    @IBOutlet var mainImage: UIImageView!
    @IBOutlet var imageBlurView: UIView!
    @IBOutlet var imageBlur: UIImageView!
    @IBOutlet var sellerUsername: UILabel!
    @IBOutlet var sellerStats: UILabel!
    
    @IBOutlet var itemTitle: UILabel!
    @IBOutlet var itemCurrentPrice: UILabel!
    @IBOutlet var itemShippingCost: UILabel!
    
    @IBOutlet var itemTimeLeft: UILabel!
    
    @IBOutlet var itemDetailsBackground: UIView!
    
    @IBOutlet var itemListingType: UILabel!
    @IBOutlet var itemNumberBidders: UILabel!
    @IBOutlet var itemNumberWatchers: UILabel!
    
    
    var context = CIContext(options: nil)
    
    
    func configureCell(item: EbayResultsModel) {
        sellerUsername.text = item._sellerUsername
        
        if (item._sellerFeedbackRating == "100.0") {
            sellerStats.text = "100% (\(item._sellerFeedbackScore!))"
        } else {
            sellerStats.text = "\(item._sellerFeedbackRating!)% (\(item._sellerFeedbackScore!))"
        }
        
        itemTitle.text = item._title
        itemCurrentPrice.text = "$\(item._currentPrice!)"
        
        if (item._shipping == "TBD") {
            itemShippingCost.text = "Shipping Unknown"
        } else if (item._shipping == "free" || item._shipping == "0.00") {
            itemShippingCost.text = "Free Shipping"
        } else {
            itemShippingCost.text = "$\(item._shipping!) S/H"
        }
        
        itemNumberBidders.isHidden = true
        
        var listingTypeString = "";
        if (item._listingType == "FixedPrice") {
            listingTypeString = "Buy It Now";
        } else if (item._listingType == "StoreInventory") {
            listingTypeString = "Buy It Now";
        } else if (item._listingType == "AuctionWithBIN") {
            listingTypeString = "Auction w/ BIN";
        } else if (item._listingType == "Auction") {
            listingTypeString = "Auction";
            self.itemNumberBidders.isHidden = false
        }

        itemListingType.text = "\(listingTypeString)"

        if (item._bestOfferEnabled == "true") {
            itemListingType.text = "\(listingTypeString) w/ BO"
        }
        
        if (item._bidCount == "1") {
            itemNumberBidders.text = "\(item._bidCount!) Bidder"
        } else {
            itemNumberBidders.text = "\(item._bidCount!) Bidders"
        }

        if (item._watchCount == "1") {
            itemNumberWatchers.text = "\(item._watchCount!) Watcher"
        } else {
            itemNumberWatchers.text = "\(item._watchCount!) Watchers"
        }
        
        
        let split = item._timeLeft.components(separatedBy: CharacterSet(charactersIn: "PDTHMS"))

        let days = Int(split[1])!
        let hours = Int(split[3])!
        let minutes = Int(split[4])!
        let seconds = Int(split[5])!
        
       
        var formatDateTime =  "\(days)d \(hours)h \(minutes)m \(seconds)s left"

        if (days >= 1) {
            if (days == 1) {
                formatDateTime = "More than \(days)d remain."
            } else {
                formatDateTime = "More than \(days)d remain."
            }
        } else {
            if (days == 0) {
                if (hours == 1) {
                    formatDateTime = "More than \(hours)h remain."
                } else {
                    formatDateTime = "More than \(hours)h remain."
                }
                //formatDateTime = String.format("%sh %sm %ss left", hours, minutes, seconds);
            }
            if (hours == 0) {
                if (minutes > 2) {
                    formatDateTime = "More than \(minutes)m remain."
                } else {
                    formatDateTime = "ACT FAST!\nLess than 2m."
                }
                //formatDateTime = String.format("%sm %ss left", minutes, seconds);
            }
            if (minutes == 0) {
                formatDateTime = "\(seconds)s left"
            }
        }


        itemTimeLeft.text = formatDateTime
                
        
        do {
            imageBlur.sd_setImage(with: URL(string: item._galleryURL), placeholderImage: UIImage(named: ""))
            mainImage.sd_setImage(with: URL(string: item._galleryURL), completed: {(image, error, type, url) in
                self.imageBlur.image = image
                if (self.imageBlur.image! != nil) { //Unexpectedly found nil while unwrapping an Optional value: file csnios/CellEbayItems.swift, line 135 HERE
                    self.blurEffect()
                }
            })
        }
        catch {
            // LOG ERROR
        }
        
        itemDetailsBackground.layer.cornerRadius = 15
    }
    
    func blurEffect() {

        let currentFilter = CIFilter(name: "CIGaussianBlur")
        let beginImage = CIImage(image: self.imageBlur.image!)
        currentFilter!.setValue(beginImage, forKey: kCIInputImageKey)
        currentFilter!.setValue(10, forKey: kCIInputRadiusKey)

        let cropFilter = CIFilter(name: "CICrop")
        cropFilter!.setValue(currentFilter!.outputImage, forKey: kCIInputImageKey)
        cropFilter!.setValue(CIVector(cgRect: beginImage!.extent), forKey: "inputRectangle")

        let output = cropFilter!.outputImage
        let cgimg = self.context.createCGImage(output!, from: output!.extent)
        let processedImage = UIImage(cgImage: cgimg!)
        self.imageBlur.image = processedImage
    }
}
letsCode
  • 2,774
  • 1
  • 13
  • 37
  • 1
    try to remove ! after ```self.imageBlur.image``` – Raja Kishan May 13 '21 at 15:20
  • Duplicate of [What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean?](https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – pkamb Oct 12 '21 at 01:04

1 Answers1

2

This line makes no sense

self.imageBlur.image! != nil

You're force-unwrapping image, which will crash the app when it's nil, and then you're comparing it with nil. Just do the check like this: self.imageBlur.image != nil

And in general, force-unwrapping outside of IBOutlets is not a good idea.

This question has a very comprehensive answer that explains how you should handle optionals in Swift.

EmilioPelaez
  • 18,758
  • 6
  • 46
  • 50