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

1 Answer 1

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)
}
Sign up to request clarification or add additional context in comments.

3 Comments

Now I feel stupid, completely overlooked that. Thanks Maxim!
@Maxim: How do I turn this String array into a single string so I can assign in to a text property of a label or something?
@Isuru I think you want to post this follow up as a new question on StackOverflow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.