DAY #53

.count() vs .isEmpty()

.count (computed property) is O(n) for non-random access collections, while .isEmpty() is O(1). Keep this in mind for performance.

Example

You have a one section table view which is either going to be empty or very full. We’ve implemented a short circuit.

1
2
3
4
let cells = []
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return cells.isEmpty() || cells.count
}

Originally published 10/08/2021 @ https://pittcsc.org/ Discord

Published here on 09/16/2022. Blog published date reflects the original date of publication.