我有一个以此方式组成的数组:
[ name, surname,gender, age, city, street, [Sons]]
Sons是一个子数组,其中将包含以下元素:
[name , surname, gender, age]
我只想得到一个有男儿子的人,但是我真的很困。我实际上在使用代码的过滤器主级别中没有问题:
self.tempArr = self.tempArr.filter({ $0?.city == "new york" })
答案 0 :(得分:0)
您确定要将所有信息放入阵列吗?为什么不为一个人创建特殊的struct /协议?
struct Person: Hashable {
enum Gender: Hashable {
case female
case male
case unknown
}
let children: [Person]
let gender: Gender
let name: String
}
let child = Person(children: [], gender: .male, name: "C")
let array = [
Person(children: [], gender: .female, name: "A"),
Person(children: [child], gender: .male, name: "B"),
]
let filtered = array.filter({ $0.children.contains(where: { $0.gender == .male }) })
编辑:
let filtered = array
.filter({ $0.children.contains(where: { $0.gender == .male }) })
.filter({ $0.children.contains(where: { $0.name == "A" || $0.name == "B" }) })