0

I might be missing something really stupid here but can someone tell me why this isn't working? nothing is getting added to the Array.

var goblin = NSImage[]()

for (var i = 2; i == 50; i++) {
   var imageString = "/Users/Neil/Developer/iOS Apps/Resources/Goblin/\(i).png"
   var image = NSImage(contentsOfFile: imageString)
   goblin.append(image)
}
goblin[0]

my files are just simply called 2.png, 3.png up to 50.png. I'm just working in playground for now

Sawyer05
  • 1,604
  • 2
  • 22
  • 37

1 Answers1

1

for loops in swift follow the same paradigm as for loops in C. That is, the CONDITION section must evaluate to true to allow the loop to follow another iteration. In your example code you seem to be aiming to achieve 49 iterations over the loop but you are starting the i counter at 2.

So on the first iteration the loop CONDITION section is evaluated by 2 == 50 which is false, therefor the loop is not executing even a single iteration.

Here is a simplification of the suggested solution:

var goblin = String[]()

for (var i = 2; i <= 50; i++) {
    var imageString = "\(i).png"

    goblin.append(imageString)
}
Maxim Veksler
  • 29,272
  • 38
  • 131
  • 151