I have a UICollectionViewController
that conforms to UICollectionViewDataSource
also by defining the following method:
override func collectionView(collectionView: UICollectionView,
cellForItemAtIndexPath indexPath: NSIndexPath)
-> UICollectionViewCell {
// ...
}
Inside that method there is an assertion that fails if execution reaches the default branch of a switch
statement. Since this is in the context of a method, an arbitrary return value is needed: nil
is my preferred choice.
switch ... {
// ...
default:
assertionFailure(); return nil
}
This would work in Objective-C but raises a compile-time error in Swift, because the contract defines a non-optional return type.
Nil is incompatible with return type 'UICollectionViewCell'
What is a best practice for handling this situation? I don't want to register/dequeue a special cell type just to satisfy the compiler. Can I somehow get by without returning any value from the default branch, or can I throw an appropriate exception instead (and if so, how)?