What does @"//" do in childWithName and when to use it

63 views Asked by At

I'm working on a SpriteKit project and my SKSpriteNode has a simple name:

node.name = @"cat"

However, when I try to do [self childWithName:@"cat"], I do not retrieve my object. Through some research I noticed some people mentioning I should be doing

[self childWithName:@"//cat"] 

and that works. I was wondering what having the "//" does?

1

There are 1 answers

1
NobodyNada On BEST ANSWER

It doesn't do special things for all NSStrings, just strings used to search the node tree, for which it recursively searches all of self's children.

From the documentation:

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.

So, for example, let's say you have this node tree:

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

Without //, childNodeWithName: would only find child1 or child2. With //, it would find child1, child2, grandchild1, grandchild2, grandchild3, or grandchild4.