- Mastering macOS Programming
- Stuart Grimshaw
- 66字
- 2021-07-02 22:54:39
Testing array elements
We can access single elements, or a subgroup of elements, according to the results of a supplied test:
let firstBiggerThan10 = arr.first(where: { $0 >= 10 })
let allBiggerThanTen = arr.filter({ $0 >= 10}
We can also easily extract maximum and minimum values from an array of a type that conforms to the Comparable protocol:
let max = arr.max()
let min = arr.min()