3 ms·
var x = [1, 2, 3]; let y = x.map( { v -> Int in print("got \(v)") x.removeAll() return 1 }) print("x: \(x)") print("y: \(y)"
by benmmurphy 7y ago
var x = [1, 2, 3];
let y = x.map( { v -> Int in
print("got \(v)")
x.removeAll()
return 1
})
print("x: \(x)")
print("y: \(y)")
got 1
got 2
got 3
x: []
y: [1, 1, 1]
I don't know much about swift but this shows the closure is able to mutate the Array. I guess value types are captured by reference by a closure.