Swift and enums #2

:  ~ 45 sec read

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) && contidionOne && !conditionTwo {
    return CGSize(width: 10, height: 20)
  }
  else if .ShippingDetails == Section(rawValue: section) && anotherCondition {
    return CGSize(width: 10, height: 40)
  }
		
  return CGSize.zeroSize
}

I think this makes the code easier to read at a glance: I can quickly scan the left side of the ifs 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: CGSize.zeroSize
  }

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.