Drakalex Drakalex - 1 year ago 105
Swift Question

Enumerate class members in Swift

I'm working with Swift and SpriteKit

I have a class :

class Button: SKShapeNode

And I have some buttons:

var button1 = Button()
var button2 = Button()

And I already use the ".name" of those nodes :

button1.name = "button1"
button2.name = "button2"

So normally, I enumerate thoses nodes with
but here, the name is already taken, so how could I enumerate all of my buttons (that use the class Button) ?

Answer Source

You can enumerate all the child nodes of a given node by calling enumerateChildNodesWithName with //* as the node name. This is listed as an example in Apple's documentation here.

Then in the block check to see if the node is of type Button and perform your action accordingly.

So something like this:

myNode.enumerateChildNodesWithName("//*") { node, ptr in
    if node is Button {
        // do something here
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download