-1

I am populating an array with core data. So I take a picture, it saves, and should return my image in a uiview, but instead getting this error:

fatal error: unexpectedly found nil while unwrapping an Optional value

I took the pic before I went into the view controller to view, it, but right when i did, it gave me this error.

    var stores = [Image]()

    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as UICollectionViewCell
    let store = stores[indexPath.row]
    var imageView = cell.viewWithTag(1) as UIImageView
    imageView.image = UIImage(data: store.image as NSData)

This code is suppossed to unwrap the collection cell with a tag of 1.

  • Do you have a class named `Image`? – luk2302 Jul 05 '15 at 07:06
  • yes, I just took care of that problem, but the fatal error is still present. – Hide Shidara Jul 05 '15 at 07:08
  • At what line does the unwrapping error occur? provide the code for that segment. – luk2302 Jul 05 '15 at 07:11
  • @HideShidara Are you using Storyboard? I'd suggest that you create a custom cell (subclass of `UICollectionViewCell`) with properties for each subview that needs to be configured (`IBOutlet` if you using xib/storyboard). – Bannings Jul 06 '15 at 02:36

1 Answers1

1

Your code doesn't unwrap the collection cell with a tag of 1. viewWithTag() returns the subview of the cell "cell" with a tag of 1. So, if that cell doesn't contain a subview with tag value 1, a nil is returned and so the error.

Gabriel
  • 3,319
  • 1
  • 16
  • 21