fatal error in a collection view

Hi,

i was following a tutorial and with the same code i have an unknown error.

The idea is to have a table view and inside a cell a collection view with a carrousel of images.

This is the code:

...

private let collectionView: UICollectionView = {

        let layout = UICollectionViewFlowLayout()

        layout.scrollDirection = .horizontal

        layout.minimumLineSpacing = 0

        layout.minimumInteritemSpacing = 0

        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)

        collectionView.register(UICollectionViewCell.self,

                                forCellWithReuseIdentifier: photoCollectionViewCell.identifier)

        return collectionView

    }()

...

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

        //let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)

        //cell.backgroundColor = .green

        

        guard let cell = collectionView.dequeueReusableCell(

            withReuseIdentifier: photoCollectionViewCell.identifier,

            for: indexPath

        ) as? photoCollectionViewCell else {

            fatalError()

        }

        cell.configure(with: images[indexPath.row])

        return cell

    }

...

Basically im getting an error in the fatalError() while casting to "photoCollectionViewCell", which is another class that contains the images to show in the carrousel. The error doesn't show any other message than the fatal error message so i have no idea why is failing.

If instead of casting to the "photoCollectionViewCell" i use the two commented lines i get the green cells so at this point im very lost.

It would be very helpful any ideas to solve this. Thanks in advance!!!

PD: sorry for my bad english

aaa

You register UICollectionViewCell.self as the class of cell to use for photoCollectionViewCell.identifier but later expect to get a cell of class photoCollectionViewCell. This fails and Swift calls the else branch where you call fatalError() (which is why there is nothing descriptive – its your fatal error, not the system's).

You should register your sell with photoCollectionViewCell.self so you get cells of the expected class back.

PS: Its customary to name types with a leading capitol letter – in this case PhotoCollectionViewCell – to distinguish them from variables (which usually start with a leading lower case letter).

fatal error in a collection view
 
 
Q