Continuing where we left off last time, is another little trick I like to use with enums
:
func collectionView(_ collectionView: UICollectionView, layout: UICollectionViewLayout, referenceSizeForFooterInSection section: NSInteger) -> CGSize {
if .products == Section(rawValue: section), conditionOne, !conditionTwo {
return CGSize(width: 10, height: 20)
}
else if .shippingDetails == Section(rawValue: section), anotherCondition {
return CGSize(width: 10, height: 40)
}
return .zero
}
I think this makes the code easier to read at a glance: I can quickly scan the left side of the if
s until I find what I'm looking for, and after that I can check for other conditions, if any. Sure, this could have been written like:
switch Section(rawValue: section)! {
case .products where conditionOne, conditionTwo:
return CGSize(width: 10, height: 20)
case .shippingDetails where anotherCondition:
return CGSize(width: 10, height: 40)
default: .zero
}
But in case you don't want to write a switch
, for whatever reason, making the if
blocks look more like one might be a big plus to readability.