2
self.enumerateChildNodesWithName(spriteName) {
        node, stop in
        // Do something with node.

}

This is an example of a bit of SpriteKit Swift code I was looking at.

It looks like the equivalent of C#'s lambda expressions, but I am not sure.

The one part I wanted to find out about is the stop in part, what is it?

I tried finding info in Swift docs, but stop in is too generic a term and couldn't find any info.

What is stop in? What is it doing?

Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
SirPaulMuaddib
  • 233
  • 2
  • 9

2 Answers2

5

The method is declared as:

func enumerateChildNodesWithName(_ name: String,
                  usingBlock block: (SKNode,
                                      UnsafeMutablePointer<ObjCBool>) -> Void)

The part in your code between { and } is a trailing closure which is the block. node and stop are the parameter names. It can be also written as:

self.enumerateChildNodesWithName(spriteName) {
        node: SKNode, stop: UnsafeMutablePointer<ObjCBool> in

See Closures in the Swift Guide.

The stop parameter is a way to stop the enumeration before enumerating all the items. See How to use "enumerateChildNodesWithName" with Swift in SpriteKit? for more information. Basically the only way to use it is setting stop.memory = true to prevent the next item to be enumerated. This is very useful if you are searching through items and you have already found the one you were looking for.

Community
  • 1
  • 1
Sulthan
  • 128,090
  • 22
  • 218
  • 270
3

You're looking at a "trailing closure" where node and stop are the two parameters passed to the closure. It's a sort of shorthand that simplifies the way you write code that takes a closure. In this case, the closure is used to evaluate each child node, so the node parameter is the node to evaluate, and stop is a parameter that lets the closure exit the enumeration early.

See Closures and scroll down to the Trailing Closures section for a more thorough description.

What is stop in? What is it doing?

If you look closely, stop and in are different colors -- in is a keyword, but stop is just an identifier. It could just as easily be ...n, s in....

Caleb
  • 124,013
  • 19
  • 183
  • 272