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

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -