I am creating collection-view with screen transition and then An error which is "Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value". I have attached picture of the error. Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

I have no idea how I fix the error. Here is My programming below.

class MyCollectionViewCell: UICollectionViewCell {
    @IBOutlet var Mylabel :UILabel!
    @IBOutlet var Myimage :UIImageView!
    
    
    
    func setup(with hero: Hero) {
        Myimage.image = hero.image
        Mylabel.text = hero.name
        
    }
}
import UIKit

struct Hero {
    let name: String
    let image: UIImage
}

let heros: [Hero] = [
    Hero(name: "1", image: #imageLiteral(resourceName: "1.png")),
    Hero(name: "2", image: #imageLiteral(resourceName: "2.png")),
    Hero(name: "3", image: #imageLiteral(resourceName: "3.png")),
    Hero(name: "4", image: #imageLiteral(resourceName: "4.png")),
    Hero(name: "5", image: #imageLiteral(resourceName: "5.png"))
]

class ViewController: UIViewController {
    
   // var herosList = [Heros]()
    @IBOutlet weak var MyCollectionView: UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        
        MyCollectionView.dataSource = self
        MyCollectionView.delegate = self
        MyCollectionView.collectionViewLayout = UICollectionViewFlowLayout()
        
        
    }

}
    
extension ViewController: UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return heros.count
    }
    
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCollectionViewCell", for: indexPath) as! MyCollectionViewCell
        cell.setup(with: heros[indexPath.row])
        return cell
    }
}
extension ViewController: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 82, height: 120)
    }
}
extension ViewController: UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let storyboard = UIStoryboard(name: "Main", bundle: nil) 
        guard let detailViewController = storyboard.instantiateViewController(withIdentifier: "DetailViewController") as? DetailViewController else {
            fatalError("Failed to instantiate DetailViewController from storyboard.")
        }
        
        detailViewController.sproduct = heros[indexPath.row]
        navigationController?.pushViewController(detailViewController, animated: true)
    }

I googled about it. But I have no idea

1

There are 1 best solutions below

0
shanimKashmiri On BEST ANSWER

It is may be because you don't have the value and you are force unwrapping it, Please try not to force unwrap until you are sure about that.