Count number of items in an array with a specific property value

前端 未结 3 1529
无人共我
无人共我 2021-02-18 18:36

I have a Person() class:

class Person : NSObject {

    var firstName : String
    var lastName : String
    var imageFor : UIImage?
    var isManager : Bool?

          


        
3条回答
  •  爱一瞬间的悲伤
    2021-02-18 18:48

    Use filter method:

    let managersCount = peopleArray.filter { (person : Person) -> Bool in
        return person.isManager!
    }.count
    

    or even simpler:

    let moreCount = peopleArray.filter{ $0.isManager! }.count
    

提交回复
热议问题