-1
我想過濾一個類的實例的數組。我想要一個新的數組通過類屬性過濾。不能完全擺脫Swift過濾器在這個用例中的工作方式。如何使用array.filter來過濾基於屬性的類對象?
enum Gender {
case male,female
}
class Person {
let name:String
let gender:Gender
init(name:String,gender:Gender) {
self.name = name
self.gender = gender
}
}
let people = [Person.init(name: "James", gender: .male),
Person.init(name: "John", gender: .male),
Person.init(name: "Sally", gender: .female)
]
let males = people.filter(something)
冠軍。非常感謝。 –