我正在寻找一种从父节点中删除所有子节点时生成异常的简单方法。
喜欢:"删除Tom,Ted,Tony,。但保持丽莎"
答案 0 :(得分:4)
您可以过滤name
属性与Lisa
不同的节点,然后将其删除
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()
}
}
答案 1 :(得分:1)
您可以使用filter
方法,使用Swift尾随闭包语法:
var children = ["Tom", "Ted", "Tony", "Lisa"]
var filtered = children.filter { $0 == "Lisa" }
// Now filtered contains only "Lisa"