I noticed that enumerating a Swift array gives all elements, even if you mutate the array during the enumeration, for example:
var a = [ 1, 2, 3 ]
for elem in a {
a.removeAll()
println(elem)
}
// Output:
// 1
// 2
// 3
Is this something that I can rely upon?
Since for-in uses the generator and it's next() method, the proper question might be: "Does the generate() method of Array capture the array contents?" It seems so, as the following also prints all three original elements:
var a = [ 1, 2, 3 ]
var gen = a.generate()
a.removeAll()
while let elem = gen.next() {
println(elem)
}