C-Style i++ for is deprecated in swift issue -
i want increment index @ point loop prints 1,3,5 want to. warning
c-style statement deprecated , ...
i know means.
for var index=0; index<5; index++ { //if condition == true index++ //else without index++ print(index) // print 1, 3, 5 } so changed to:
for var index in 0..<5 { //if condition == true index += 1 //else without index++ print(index) // print 1,2,3,4,5 should 1,3,5 side } i wondering why index not mutable? though have set var or solutions issue.
the index not mutable because
for var x in y { ... } is equivalent to
for temp in y { var x = temp ... } where var makes x copy of temp. when modify x, won't modify real index temp (this reason why se-0003 introduced)
the c-style for loop can reduced while loop:
var index = 0 while index < 5 { if conditiona { index += 1 } print(index) index += 1 }
Comments
Post a Comment