计算具有特定属性值的数组中的项目数 [英] Count number of items in an array with a specific property value
本文介绍了计算具有特定属性值的数组中的项目数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Person()类:
I have a Person() class:
class Person : NSObject {
var firstName : String
var lastName : String
var imageFor : UIImage?
var isManager : Bool?
init (firstName : String, lastName: String, isManager : Bool) {
self.firstName = firstName
self.lastName = lastName
self.isManager = isManager
}
}
我有一个Person()数组
I have an array of Person()
var peopleArray = [Person]()
我想计算数组中拥有
isManager: true
我觉得这已经存在了,但是我找不到它,或者找不到搜索参数.
I feel this is out there, but I can;t find it, or find the search parameters.
谢谢.
推荐答案
使用filter
方法:
let managersCount = peopleArray.filter { (person : Person) -> Bool in
return person.isManager!
}.count
或更简单:
let moreCount = peopleArray.filter{ $0.isManager! }.count
这篇关于计算具有特定属性值的数组中的项目数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文