0

I've collection view and cell configured from array, I want to set the image of cell's UIImageView from the array, but xCode returns an error saying: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value, I don't know if it's helpful, but I can't even open the tab where collectionView and its cell is located, the app crashes immediately

//collectionViewCell
    @IBOutlet weak var headerTitle: UILabel!
    @IBOutlet weak var infoLabel: UILabel!
    @IBOutlet weak var countryImage: UIImageView!
    func config(data:SearchCollectionViewData){
        self.headerTitle.text = data.header
        self.infoLabel.text = data.information
        self.countryImage.image = data.image // error goes here
    }
//collectionView and its configuration
    var array2:[SearchCollectionViewData] = [
        SearchCollectionViewData(header: "Georgia,Tbilisi Airport", information: "Georgia is located between Asia and Europe and occupies a land area of 69,700 square kilometres, bordered by the Black Sea to the west, Turkey to the southwest, Azerbaijan to the east, Russia to the north, and Armenia to the south. ... Georgian is the official language of Georgia, and it is spoken by 71% of the population.", image: UIImage(named: "tbtbtb")!)
]

    override func viewDidLoad() {
        super.viewDidLoad()
        
        collectionView.delegate = self
        collectionView.dataSource = self
}
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "searchCell", for: indexPath) as! searchCollectionViewCell
            cell.config(data: array2[indexPath.row])
}

5
  • 1
    By using exception breakpoints: At what line does the crash occur? Commented Feb 10, 2022 at 15:11
  • Consider each of the ! in code: one of your outlets might not be hooked up properly, your image name might be incorrect, the dequeued cell type might not be what you expect. Commented Feb 10, 2022 at 15:15
  • Do you have this image in your assets UIImage(named: "tbtbtb")? Commented Feb 10, 2022 at 15:19
  • @DonMag yes i do Commented Feb 10, 2022 at 17:56
  • @meaning-matters with breakpoints, code crashes on @IBOutlet weak var headerTitle: UILabel! line in collectionViewCell Commented Feb 10, 2022 at 18:00

1 Answer 1

1

The main problem was @IBOutlet weak var countryImage: UIImageView!, where the error was, it wasn't connected to storyboard

Sign up to request clarification or add additional context in comments.

1 Comment

Great you found it. This is indeed common mistake that's easily made.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.