标签: ios swift
在Swift中,我如何遍历整数var numbers = [4,5,5,4,3]的NSMutableArray并计算有多少等于5?
var numbers = [4,5,5,4,3]
答案 0 :(得分:4)
您可以使用reduce:
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