@“//”在childWithName中做什么以及何时使用它

时间:2015-06-18 22:11:13

标签: ios nsstring sprite-kit sknode

我正在开发SpriteKit项目,而我的SKSpriteNode名称简单:

node.name = @"cat"

但是,当我尝试[self childWithName:@"cat"]时,我不会检索我的对象。通过一些研究,我注意到有些人提到我应该做的事情

[self childWithName:@"//cat"] 

这是有效的。我想知道“//”的作用是什么?

1 个答案:

答案 0 :(得分:4)

它不会对所有NSString做特殊的事情,只是用于搜索节点树的字符串,它以递归方式搜索所有self个孩子。

来自文档:

  

当放置在搜索字符串的开头时,此[//]指定搜索应从根节点开始,并在整个节点树中递归执行。它在搜索字符串中的任何其他位置都不合法。

因此,举例来说,假设你有这个节点树:

                 scene
              /         \
             /           \
        child1              child2
       /      \              /     \
      /        \            /        \
grandchild1  grandchild2 grandchild3  grandchild4 

如果没有//childNodeWithName:只会找到child1child2。使用//,它会找到child1child2grandchild1grandchild2grandchild3grandchild4