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

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

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