-3

Am new to swift and learning I use " for(SKNode * node in nodes) " for selecting the particular node with its name in objective-C but in swift I need some help to do this. Thanks in advance

Iniyan
  • 55
  • 6
  • 4
    Just read the documentation: [For Loops](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ControlFlow.html#//apple_ref/doc/uid/TP40014097-CH9-ID121) – Antonio Mar 02 '15 at 10:12

3 Answers3

1
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
            let touch = touches.anyObject() as UITouch
            let touchLocation = touch.locationInNode(self)
            let nodes = self.nodesAtPoint(touchLocation) as [SKNode]

            for node in nodes {
                if let nodeName = node.name {
                    if nodeName == "myNodeName" {
                        println("node tapped")
                    }
                }
            }
        }

To check if a node is tapped, iterate with for loop. Loop through the SKNodes, and check if the node name matches. Difference is that instead of (SkNode* node) we have :

let nodes = self.nodesAtPoint(touchLocation) as [SKNode]
yaksha
  • 98
  • 3
0

in SWIFT

for node in nodes {

}
Huynh Inc
  • 2,010
  • 1
  • 25
  • 42
0

Here is a simple example using "for" loop in swift :

    let arrStrings = ["one","two","three","four"]

    for str in arrStrings
    {
       if str=="two"
       {
         println("\(str)")
       }
    }
user4261201
  • 2,324
  • 19
  • 26