1

I'm a noobie in Swift. I'm trying to iterate over SKNodeTree and check if there are scary monster Nodes here. However I cannot figure out how to typecase the for loop. I have understood that this would be possible with "as" clause.

By the way, is comparing strings with == ok in Swift?

      for monsterNode in self.children{
          if (monsterNode.name? == "scary") {
                println("scary monster here")
          }
      }
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
user3673836
  • 591
  • 1
  • 9
  • 23
  • See [how to count number of sprites swift](http://stackoverflow.com/questions/27201797/how-to-count-number-of-sprites-swift) for an easier solution. – Martin R Dec 08 '14 at 16:16

1 Answers1

2

Comparing strings can be done by using == instead of isEqualToString, so thats fine. Your code should be like this:

for monsterNode in self.children as [SKNode] {
    if (monsterNode.name? == "scary") {
          println("scary monster here")
    }
}

You can submit your cast inside the brackets []

Antoine
  • 23,526
  • 11
  • 88
  • 94