2020-03-05 Capping an array in Swift

Do you want to cap an array in Swift to a certain size? Here's how:

    var array = ["a", "b", "c", "d", "e"]
    array.replaceSubrange(3..., with: [])

The array now contains three items. It'll crash when instead of 3, you enter an index that's beyond the array count. If you wish to avoid that, do something like the following:

    array.replaceSubrange([0, array.count, 3].sorted()[1]..., with: [])