0

TableView default function:

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

    let post = posts[(indexPath as NSIndexPath).row]

    if let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell") as? PostCell {

        cell.request?.cancel()
        var img: UIImage?

        if let url = post.imageUrl {
            img = FeedVC.imageCache.object(forKey: url as AnyObject) as? UIImage
        }

        cell.configureCell(post, img: img)

        return cell
    } else {
        return PostCell()
    }
}

Configure Cell function:

func configureCell(movie: Movie, img: UIImage?){

    self.movieTitle.text = movie.title
    self.movieCaption.text = movie.caption

    if movie.imgURL != nil {

        if img != nil {
            self.movieImg.image = img
        }
        else {

            let url = movie.imgURL

            request = Alamofire.request(url!).response { response in
                let img = UIImage(data: response.data!)! //always returns with "fatal error: unexpectedly found nil while unwrapping an Optional value"

                self.movieImg.image = img
                ViewController.imageCache.setObject(img, forKey: url as AnyObject)
            }

        }
    }
    else {
        self.movieImg.isHidden = true
    }


}

For some reason, whenever I run this code, it always crashes and the output is:

fatal error: unexpectedly found nil while unwrapping an Optional value.

This is on the configure cell function when I set the image to UIImage(data). I have tried many scenarios but am failing to figure out what my problem is.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    Possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – rmaddy Feb 08 '17 at 03:21
  • just print `data` after you get it, likely it's `nil` and you're crashing because of it. – mfaani Feb 08 '17 at 03:46
  • @Honey how do you think i would go about my code differently then to make it run. Like how could i word the function to make data have a value. When i print "response.data", my output is "Optional(0 bytes)" – Shaan Luthra Feb 08 '17 at 03:48
  • all that your error means is that you have hit `nil` when you totally didn't expect it. It's either that the `data` is `nil` or that no UIImage could have been initialized from that `data` and then you got `nil` as its data – mfaani Feb 08 '17 at 03:53
  • @Honey yes, the data is nil. IS there any way you think there is that i should change my code? – Shaan Luthra Feb 08 '17 at 04:11
  • @ShaanLuthra it means ur network call has no **data** returned instead it has an **error** returning. Likely there is something wrong with your url I guess – mfaani Feb 08 '17 at 12:15
  • @Honey when i put the url into the browser and hit enter it pops up with the correct image though – Shaan Luthra Feb 08 '17 at 21:34

0 Answers0