-1

I am taking an online course to learn Swift. I followed the code in the videos, but I am getting an error. It is coming from the line where I am registering a xib for a collection view. The code in the video has "bundle: nil", but that is where I am getting the:

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

Where am I going wrong?

Here is my code:

@IBOutlet weak var collectionView: UICollectionView!

override func awakeFromNib() {
    super.awakeFromNib()
    collectionView.register(UINib(nibName: "StoryCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "StoryCollectionViewCell")
}

My code

Cœur
  • 37,241
  • 25
  • 195
  • 267
Natasha
  • 3
  • 1

1 Answers1

0

It's failing because the collectionView you're calling the method on is nil. In your image, to the left of the @IBOutlet weak var collectionView: UICollectionView! line is an empty circle. When that circle is empty (has no dot in it), it means it is not connected to your XIB/Storyboard file, so it's nil when you are trying to run code against it.

In order to connect it, you can open up the storyboard side by side with your code, click the circle and drag it to your collectionView in the XIB/Storyboard file you have. See this documentation for more info on connecting views from your XIB/Storyboard files to code: https://developer.apple.com/library/archive/documentation/ToolsLanguages/Conceptual/Xcode_Overview/ConnectingObjectstoCode.html

johnny
  • 1,434
  • 1
  • 15
  • 26