繁体   English   中英

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

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM