【问题标题】:What does @"//" do in childWithName and when to use it@"//" 在 childWithName 中做了什么以及何时使用它
【发布时间】:2015-06-18 22:11:13
【问题描述】:

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

node.name = @"cat"

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

[self childWithName:@"//cat"] 

这行得通。我想知道“//”有什么作用?

【问题讨论】:

    标签: ios nsstring sprite-kit sknode


    【解决方案1】:

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

    来自文档:

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

    因此,例如,假设您有这个节点树:

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

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

    【讨论】:

    • 太棒了。几分钟前我刚刚遇到了答案,但您的答案更完整。谢谢!
    猜你喜欢
    • 2013-07-28
    • 2021-11-12
    • 2017-04-20
    • 2023-03-06
    • 2012-07-14
    • 2010-11-11
    • 2012-04-26
    • 2020-07-29
    相关资源
    最近更新 更多