UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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

Forums > Swift

ik know it had something to do with the iboutlet collectionView but i cant figure it out can sombody please help

 @IBOutlet var collectionView: UICollectionView!

        let headerId = "headerId"
        let arrImages = ["image1", "test"]
        let categoryHeaderId = "categoryHeaderId"

    override func viewDidLoad() {
           super.viewDidLoad()

           self.navigationItem.title = "The Closet"
           view.backgroundColor = .red
           collectionView.register(brandCollectionViewCell.self, forCellWithReuseIdentifier: brandCollectionViewCell.reuseIdentifer)
           collectionView.register(brandCollectionReusableView.self, forSupplementaryViewOfKind: categoryHeaderId, withReuseIdentifier: headerId)

           collectionView.collectionViewLayout = createCompositionalLayout()

           collectionView.reloadData()
       }

       //MARK: - Helper Method
       private func createCompositionalLayout() -> UICollectionViewCompositionalLayout {

           return UICollectionViewCompositionalLayout { (sectionNumber, env) -> NSCollectionLayoutSection? in

               switch sectionNumber {

               case 0: return self.firstLayoutSection()
               case 1: return self.secondLayoutSection()
               default: return self.thirdLayoutSection()
               //case 2: return self.thirdLayoutSection()
               //default: return self.fourthLayoutSection()
               }
           }
       }

       private func firstLayoutSection() -> NSCollectionLayoutSection {

           let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1))

           let item = NSCollectionLayoutItem(layoutSize: itemSize)
           item.contentInsets.bottom = 15

           let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.9), heightDimension: .fractionalWidth(0.5))

           let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
           group.contentInsets = .init(top: 0, leading: 15, bottom: 0, trailing: 2)

           let section = NSCollectionLayoutSection(group: group)

           section.orthogonalScrollingBehavior = .groupPaging

           return section
       }

       private func secondLayoutSection() -> NSCollectionLayoutSection {

           let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.33), heightDimension: .absolute(100))

           let item = NSCollectionLayoutItem(layoutSize: itemSize)
           item.contentInsets = .init(top: 0, leading: 0, bottom: 15, trailing: 15)

           let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .estimated(500))

           let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])

           let section = NSCollectionLayoutSection(group: group)
           section.contentInsets.leading = 15

           section.boundarySupplementaryItems = [
               NSCollectionLayoutBoundarySupplementaryItem(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .estimated(44)), elementKind: categoryHeaderId, alignment: .top)
           ]

           return section
       }

       private func thirdLayoutSection() -> NSCollectionLayoutSection {

           let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1))

           let item = NSCollectionLayoutItem(layoutSize: itemSize)
           item.contentInsets.bottom = 15

           let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.8), heightDimension: .fractionalWidth(0.35))

           let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
           group.contentInsets = .init(top: 0, leading: 15, bottom: 0, trailing: 2)

           let section = NSCollectionLayoutSection(group: group)

           section.orthogonalScrollingBehavior = .continuous

           return section
       }

       private func fourthLayoutSection() -> NSCollectionLayoutSection {

           let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.5), heightDimension: .fractionalWidth(0.5))

           let item = NSCollectionLayoutItem(layoutSize: itemSize)
           item.contentInsets = .init(top: 0, leading: 0, bottom: 15, trailing: 15)

           let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .estimated(500))

           let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
           group.contentInsets.leading = 15

           let section = NSCollectionLayoutSection(group: group)

           return section
       }
   }

   //MARK: - UICollectionViewDataSource Methods
   extension brandViewController: UICollectionViewDataSource {

       func numberOfSections(in collectionView: UICollectionView) -> Int {

           return 1
       }

       func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

           switch section {

               case 0:
                   return 3
               case 1:
                   return 1
               default:
                   return 1
           }
       }

       func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

           let cell = collectionView.dequeueReusableCell(withReuseIdentifier: brandCollectionViewCell.reuseIdentifer, for: indexPath) as! brandCollectionViewCell

        if let name = arrImages.randomElement() {

                    cell.configure(withImageName: name)
                }
           return cell
       }

       func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

           let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerId, for: indexPath)

           return header
       }
   }

2      

Is your collectionView actually connected to the storyboard?

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.