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:
- your logic backwards. want print while
x
less10
, so: - 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
Post a Comment