0
votes

I am using latest stable Swift inside my function i get fatal error.when i ran my ios app.

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

I searched for answer, but they offer different methods for unwrapping it.

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! UICollectionViewCell
        var imageView = cell.viewWithTag(1) as! UIImageView
        imageView.image = UIImage(named: imagesArray[indexPath.row])
        }
1

1 Answers

1
votes

You can use an if let block to ensure the value is not nil before trying to use it

if let imageView = cell.viewWithTag(1) as? UIImageView {
    imageView.image = UIImage(named: imagesArray[indexPath.row])
}