@"//" 在 childWithName 中做什么以及何时使用它
What does @"//" do in childWithName and when to use it
我正在做一个 SpriteKit 项目,我的 SKSpriteNode 有一个简单的名字:
node.name = @"cat"
但是,当我尝试执行 [self childWithName:@"cat"]
时,我没有检索到我的对象。通过一些研究,我注意到有些人提到我应该这样做
[self childWithName:@"//cat"]
那行得通。我想知道“//”有什么用?
它不会对所有 NSString
做特殊的事情,只是用于搜索节点树的字符串,它会递归搜索 self
的所有子节点。
来自文档:
When placed at the start of the search string, this [//
] specifies that the search should begin at the root node and be performed recursively across the entire node tree. It is not legal anywhere else in the search string.
因此,例如,假设您有这个节点树:
scene
/ \
/ \
child1 child2
/ \ / \
/ \ / \
grandchild1 grandchild2 grandchild3 grandchild4
如果没有 //
,childNodeWithName:
只会找到 child1
或 child2
。使用 //
,它会找到 child1
、child2
、grandchild1
、grandchild2
、grandchild3
或 grandchild4
.
我正在做一个 SpriteKit 项目,我的 SKSpriteNode 有一个简单的名字:
node.name = @"cat"
但是,当我尝试执行 [self childWithName:@"cat"]
时,我没有检索到我的对象。通过一些研究,我注意到有些人提到我应该这样做
[self childWithName:@"//cat"]
那行得通。我想知道“//”有什么用?
它不会对所有 NSString
做特殊的事情,只是用于搜索节点树的字符串,它会递归搜索 self
的所有子节点。
来自文档:
When placed at the start of the search string, this [
//
] specifies that the search should begin at the root node and be performed recursively across the entire node tree. It is not legal anywhere else in the search string.
因此,例如,假设您有这个节点树:
scene
/ \
/ \
child1 child2
/ \ / \
/ \ / \
grandchild1 grandchild2 grandchild3 grandchild4
如果没有 //
,childNodeWithName:
只会找到 child1
或 child2
。使用 //
,它会找到 child1
、child2
、grandchild1
、grandchild2
、grandchild3
或 grandchild4
.