How can I find a number of True statements in an Array of Bools in Swift

前端 未结 2 511
忘掉有多难
忘掉有多难 2020-12-11 03:03

I am a new developer and cannot seem to figure out how to find the number of True statements in an Array of Booleans. I know how to find by index but not by value. Any assis

2条回答
  •  失恋的感觉
    2020-12-11 03:25

    Since you are dealing with an array of arrays, the computation will have two steps as well. First, use map to convert each inner array to its truth count, and then aggregate with reduce to obtain the final result:

    let countTrue = before.map {$0.filter{$0}.count}.reduce(0, +)
    

提交回复
热议问题