Swift:遍历数组并计算 Int 等于 5
Swift: Iterate through array and count Ints equal to 5
在 Swift 中,我将如何遍历整数 var numbers = [4,5,5,4,3]
的 NSMutableArray 并计算有多少等于 5?
一种可能的解决方案:
numbers.filter {[=10=] == 5}.count
您可以为此使用 reduce
:
let array = [4,5,5,4,3]
let fives = array.reduce(0, combine: { [=10=] + Int( == 5) })
在 Swift 中,我将如何遍历整数 var numbers = [4,5,5,4,3]
的 NSMutableArray 并计算有多少等于 5?
一种可能的解决方案:
numbers.filter {[=10=] == 5}.count
您可以为此使用 reduce
:
let array = [4,5,5,4,3]
let fives = array.reduce(0, combine: { [=10=] + Int( == 5) })