Learn Courses My Dashboard

Match Game Fatal Error

Hi I’m on lesson 5 of the match game app and I keep getting an error saying

Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

It’s on line 20 in my ViewController right after

collectionView.delegate = self

Just wondering if anyone else is having this same issue

Welcome to the community! Can you paste the code you have and comment where the error is?

Paste your code in as text, rather than providing a screenshot.

To format the code nicely, place 3 back-ticks ``` on the line above your code and 3 back-ticks ``` on the line below your code. The 3 back-ticks must be the ONLY characters on the line. The back-tick character is located on the same keyboard key as the tilde character ~ (which is located below the Esc key). You can also highlight an entire code block and click the </> button on the toolbar to wrap the block for you.

This also makes it easier for anyone assisting as they can copy the code and carry out some testing.

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    var model = CardModel()
    var cardArray = [Card]()

    override func viewDidLoad() {
        super.viewDidLoad()
        
// Thread 1 Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional Value 
        collectionView.delegate = self
        collectionView.dataSource = self
       
        // Call the getCards method of the card model
        cardArray = model.getCards()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK: - UICollectionView Protocol Methods
    
    func collectionView(_ collectionView: UICollectionView,
        numberOfItemsInSection section: Int) -> Int {
        
        return cardArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView,
        cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell =
            collectionView.dequeueReusableCell(withReuseIdentifier:
            "CardCell", for: indexPath)
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView,
        didSelectItemAt indexPath: IndexPath) {
        
    }
}

The code you have there ought to work though Lesson 6 covers the method to connect the collectionView to ViewController.

When you observe your code in your ViewController.swift file, have a look at the line:

@IBOutlet weak var collectionView: UICollectionView!

and as per the attached image, let me know if there is a filled circle like that indicated by the arrow?

If it is not filled, then it is possible that there is a break between the ViewController.swift file and the storyboard objects.

Another check to make is to look at your storyboard and select the View Controller and Right Click on the left icon in the Bar at the top of the View Controller.

This will reveal the connections that exist between that View Controller object and the ViewController.swift file.

If you see an Amber triangle to the right (as indicated by the arrow) that mean that the connection has been lost.

If that is the case then you will need to delete that connection by tapping the little x as indicated and then reconnect the collectionView to the ViewController.swift file as you would have done in the earlier lesson.

Thank you so much for your help! I had to re attach the IB outlet :slight_smile:

1 Like