0

i am trying to implement the collection vie as described in this tutorial

it is crashing on the following line

let gelleryImageView: UIImageView = (cell.viewWithTag(100) as! UIImageView)

with

fatal error: unexpectedly found nil while unwrapping an Optional value

my imageView is the only child of the reusable cell and the imageView has the tag of 100

enter image description here

enter image description here

Rest of method

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)

    // Configure the cell
    print(cell.subviews.count)


    let gelleryImageView: UIImageView = (cell.viewWithTag(100) as! UIImageView)

    gelleryImageView.image = images[indexPath.row]

    return cell
}
Lonergan6275
  • 1,938
  • 6
  • 32
  • 63
  • 1
    a tutorial recommending you to use tags (and not telling you why it's a bad idea) is a bad tutorial. how have you checked the tag is 100? – Wain Mar 07 '16 at 14:48
  • It was set in interface builder in the attributes inspector – Lonergan6275 Mar 07 '16 at 14:50
  • you said it is 100, i asked how you know it's 100, did you check it at runtime? your error suggests it isn't actually 100. perhaps the image view isn't even a subview... – Wain Mar 07 '16 at 14:53
  • yes sorry misread your comment. I have added 2 screenshots if that helps. – Lonergan6275 Mar 07 '16 at 14:54
  • you need to debug and check the truth at runtime - probably you're either editing the tag or instantiating the cell from something other than the NIB / storyboard – Wain Mar 07 '16 at 14:57

2 Answers2

1

That error tells you that something you are force-unwrapping with "!" is actually nil.

Until you get used to optionals, don't use force-unwrap. It's dangerous.

Instead, use "if let" optional binding, or a guard statement, or some other approach that lets you gracefully fail if the optional contains a nil.

In your case it is likely that there is no view with the tag of 100, so it's returning nil. If you didn't use force-unwrapping you'd be able

See this thread I created covering the topic:

How to deal with "Fatal error: unexpectedly found nil while unwrapping an Optional value."

Community
  • 1
  • 1
Duncan C
  • 128,072
  • 22
  • 173
  • 272
0

You shouldn't be accessing the cell image via tag. Create a custom cell class and create an outlet for the imageView and access it that way.

remove line from viewdidload()

self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

//

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! GalleryCollectionViewCell

    cell.galleryImage.image = images[indexPath.row]

    return cell
}
Lonergan6275
  • 1,938
  • 6
  • 32
  • 63
DookieMan
  • 992
  • 3
  • 10
  • 26