[英]Is there a way to iterate through elements in an array in Swift, by incrementing the count with a UIButton action?
[英]Swift: Iterate through array and count Ints equal to 5
在Swift中,我将如何遍历int整数var numbers = [4,5,5,4,3]
的NSMutableArray并计算有多少等于5?
您可以为此使用reduce
:
let array = [4,5,5,4,3]
let fives = array.reduce(0, combine: { $0 + Int($1 == 5) })
一种可能的解决方案:
numbers.filter {$0 == 5}.count
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.