swift - how to retain the value of the last iteration on a sequence in for-in -
var index: int=0 index in 1...3{ print(index) } print(index)//prints 0
if run code, last print gives 0, means index inside for-in not same outside. seems force declares new constant.
looking similar way retain last value in sequence
i know can
var index_out: int=0 index in 1...3{ print(index) index_out = index } print(index_out)
if you're gonna loop through , want know end index use amount of times looped through it:
let n = 3 index in 1...n{ print(index) } print(n)
or array:
let array = [int](count: 10, repeatedvalue: 2) index in 0..<array.count { print(index) } print(array.count)
Comments
Post a Comment