2

I am currently doing a course for Swift.

While trying to write a simple while loop like the following:

var counter = 0

while counter < 10 {
   counter += 1
   print(counter)
}

This just returns on console 0, 0, 0, 0... non stop. So the programs doesn't add 1 to the counter, and consequently does not stop when counter < 10.

Why does this happen? I also tried to change it with:

counter = counter + 1 

but the situation does not change. I leave a screenshot of my Playground

Playground Issue

zneak
  • 134,922
  • 42
  • 253
  • 328
  • 1
    Cannot reproduce. Is that your actual code? – Martin R Jun 13 '18 at 20:13
  • Yes, that is the exact code retrieved from the course and from my program as well. – Alessio Longhi Jun 13 '18 at 20:21
  • Is this a Playground or a command-line program? What happens if you create a new fresh command-line program in Xcode, copy/paste that code, and run it? – Martin R Jun 13 '18 at 20:24
  • I've edited your question to specify that it is playgrounds-related, since as a "regular" Swift program, this is valid and works as you expect. – zneak Jun 13 '18 at 20:31
  • I cannot reproduce it in a Playground either. Have you restarted Xcode, created a new Playground? – Martin R Jun 13 '18 at 20:32
  • 1
    (possibly related: https://stackoverflow.com/questions/33293442/get-playground-to-display-all-loop-results) – zneak Jun 13 '18 at 20:34
  • Yes, I already did. And with a command-line program it works. – Alessio Longhi Jun 13 '18 at 20:37
  • 1
    Perhaps a problem with the dark background – just kidding :) – Martin R Jun 13 '18 at 20:39
  • I tried to change the "Theme" of Xcode, and it just works as a theme. Also, the post share by zneak is not related as it just shows the history of the program ( that in my case is 0 repeated multiple times ) – Alessio Longhi Jun 13 '18 at 20:40
  • 1
    If restarting Xcode hasn't fixed the issue for you, I'd try clearing the DerivedData folder: https://stackoverflow.com/a/38043112/1646862. It seems as if the playground was compiled before you had added `counter += 1` but not after – Hristo Jun 13 '18 at 21:25
  • Thank you Hristo, this solved the issue. – Alessio Longhi Jun 13 '18 at 21:54

0 Answers0