Mastering macOS Programming
上QQ阅读APP看书,第一时间看更新

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()