swift - Using a Closure as a While Loop's Condition -


i want use closure condition while loop. have:

var x: int = 0 var closure = {() -> bool in return x > 10} while closure {     x += 1     println(x) // never prints } 

it never prints anything. if change closure(), doesn't work either.

any appreciated.

two issues:

  1. your logic backwards. want print while x less 10, so:
  2. when call closure directly function, i.e. parenthesis.

updated code, tested swift 2.0:

var x: int = 0 var closure = {() -> bool in return x < 10} while closure() {     x += 1     print(x) } 

Comments

Popular posts from this blog

android - questions about switching from C2DM to GCM -

c++ - Qt setGeometry: Unable to set geometry -

batch file - How to extract all multi-volume RAR archives from subfolders of a folder? -