Crash setting UICollectionView collectionViewLayout

I have a

UICollectionView
which should support setting
collectionViewLayout
to different types of layouts (two right now). When I first change the layout, there's no issue. The issue is when I want to reset it back after that layout change. It crashes on
collectionView.setCollectionViewLayout(firstLayout, animated: false)
whether or not I call
collectionView.collectionViewLayout.invalidateLayout()
before. Sometimes I get a
EXC_BAD_ACCESS
and other times I get
signal SIGABRT
and a console message:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[_UIFlowLayoutSection center]: unrecognized selector sent to class 0x10f86b408'

It doesn't seem to be

firstLayout
's fault. Also, the crash doesn't happen on every test but usually does.

Here's the function where I update the layout based on

currentChoice
:


let firstLayout = UICollectionViewFlowLayout()
let secondLayout = UICollectionViewFlowLayout()

func updateCollectionView() {
        switch currentChoice {
        case .vegetables, .toppings, .dressings:
            collectionView.alwaysBounceHorizontal = false
            collectionView.alwaysBounceVertical = true
            collectionView.isPagingEnabled = false
        
            collectionView.collectionViewLayout.invalidateLayout()
            collectionView.setCollectionViewLayout(firstLayout, animated: false)
        default:
            collectionView.alwaysBounceHorizontal = true
            collectionView.alwaysBounceVertical = false
            collectionView.isPagingEnabled = true
        
            collectionView.collectionViewLayout.invalidateLayout()
            collectionView.setCollectionViewLayout(secondLayout, animated: false)
        }
}


The code is pretty simple. It really seems like there's just a bug with

UICollectionView
or something.