I have a model that holds a bool value. I would like to return a UICollectionViewCell based on the value of the bool within the model using a switch statement. However, I am getting a casting error.
Could not cast value of type 'Project.FalseCell' (0x10077a9f0) to 'Project.TrueCell' (0x100777ef8).
I've checked the following;
- I have registered my cells in
viewDidLoad
- I have unique identifiers for each cell type
What am I missing here?
// MODEL
struct Model {
let bool: Bool
}
// CONTROLLER
let models = [Model]()
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let model = models[indexPath.item]
switch model.bool {
case true:
let trueCell: TrueCell = listView.collectionView.dequeueReusableCell(withReuseIdentifier: TrueCell.identifier, for: indexPath) as! TrueCell
return trueCell
case false:
let falseCell: FalseCell = listView.collectionView.dequeueReusableCell(withReuseIdentifier: FalseCell.identifier, for: indexPath) as! FalseCell
return falseCell
}
}
As per @goat_herd 's comment, there was an issue with using cell identifiers as
String(describing: Self)
as I had renamed the Class at one point and this caused a disruption in the identifier.I changed the identifier to a raw valued string and this solved the issue.