Swift:遍历数组并计算Ints等于5

时间:2015-01-20 08:20:30

标签: ios swift

在Swift中,我如何遍历整数var numbers = [4,5,5,4,3]的NSMutableArray并计算有多少等于5?

2 个答案:

答案 0 :(得分:4)

您可以使用reduce

let array = [4,5,5,4,3]
let fives = array.reduce(0, combine: { $0 + Int($1 == 5) })

答案 1 :(得分:1)

一种可能的解决方案:

numbers.filter {$0 == 5}.count
相关问题