cellHeights is an array containing optional CGFloat . Thus, any of its elements cannot be nil, as such, if an index exists, the element of this index is CGFloat .
What you are trying to do is what is possible only if you create an array of options :
var cellHeights: [CGFloat?] = [CGFloat?]()
and in this case, the optional binding should be used as follows:
if let height = cellHeights[index] { cellHeights[index] = cell.frame.size.height } else { cellHeights.append(cell.frame.size.height) }
I suggest you read Options again
source share