Let's say I have this
var array = [1,2,3]
array.forEach {
array.append(17)
print($0)
}
It will not loop forever, instead it will print 1, 2 and 3 and afterwards the array has 6 elements: [1,2,3,17,17,17]
How can this be understood?
the array inside the block is a copy of the array the moment i call .append(17), right? And then after the loop it's assigned to the array var? Or is the copy made before hand?
which is actually what is going on?
This:
var array = [1,2,3]
var arrayCopy: [Int]
array.forEach {
arrayCopy.append(17)
print($0)
}
array = arrayCopy
or this:
var array = [1,2,3]
var arrayCopy = array
arrayCopy.forEach {
array.append(17)
print($0)
}
Or something else?