Reputation: 637
I'm looking for a simple way to make exceptions when removing all children from its parent Node.
Like: "Remove Tom, Ted, Tony,. but keep Lisa"
Upvotes: 0
Views: 282
Reputation: 59496
You can filter the nodes having the name
property different from Lisa
and then removing them
self.children.filter { $0.name != "Lisa" }.forEach { $0.removeFromParent() }
let keepTheseNames = Set(["Lisa", "Tom"])
self.children.forEach {
if let name = $0.name where !keepTheseNames.contains(name) {
$0.removeFromParent()
}
}
Upvotes: 4
Reputation: 1028
You can use filter
method, with Swift trailing closure syntax:
var children = ["Tom", "Ted", "Tony", "Lisa"]
var filtered = children.filter { $0 == "Lisa" }
// Now filtered contains only "Lisa"
Upvotes: 1