I have a Person() class:
class Person : NSObject {
var firstName : String
var lastName : String
var imageFor : UIImage?
var isManager : Bool?
count(where:)
was removed from Swift 5 in Xcode 10.2 beta 4.
With Swift 5 and Xcode 10.2 beta 3, you can use Array
's count(where:) method if you want to count the number of elements in an array that match a given predicate. count(where:)
has the following declaration:
func count(where predicate: (Element) throws -> Bool) rethrows -> Int
Returns the number of elements in the sequence that satisfy the given predicate.
The following Playground sample code shows how to use count(where:)
:
struct Person {
let name: String
let isManager: Bool
}
let array = [
Person(name: "Jane", isManager: true),
Person(name: "Bob", isManager: false),
Person(name: "Joe", isManager: true),
Person(name: "Jill", isManager: true),
Person(name: "Ted", isManager: false)
]
let managerCount = array.count(where: { (person: Person) -> Bool in
return person.isManager
})
// let managerCount = array.count { $0.isManager } // also works
print(managerCount) // prints: 3