user3138007
user3138007

Reputation: 637

How to make exceptions when removing all children from a member of SKNode?

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

Answers (2)

Luca Angeletti
Luca Angeletti

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() }

How could I add more names to the filter? ..like keep Lisa AND Tom?

let keepTheseNames = Set(["Lisa", "Tom"])

self.children.forEach {
    if let name = $0.name where !keepTheseNames.contains(name)  {
        $0.removeFromParent()
    }
}

Upvotes: 4

Evgeny Sureev
Evgeny Sureev

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

Related Questions