var absences = [0, 2, 0, 4, 0, 3, 1, 0]
let midpoint = absences.count / 2
var firstHalf = absences.prefix(upTo: midpoint)
let secondHalf = absences.suffix(from: midpoint)
Quotation from Apple:
Neither the firstHalf nor secondHalf slices allocate any new storage of their own. Instead, each presents a view onto the storage of the absences array.
When I try to mutate firstHalf
as the following:
firstHalf[1] = 19
the values of firstHalf
changes but the original array absences
remains the same (firstHalf[1]
is equal to 19 while absences[1]
equals to 2)
So what happens in the background. Did I instantiate a new array by mutating the array slice?
Thanks in advance.