Skip to content

Instantly share code, notes, and snippets.

@jplazcano87
Created August 16, 2018 13:25
Show Gist options
  • Save jplazcano87/f01a305fde64233397c45d2a6ed3a213 to your computer and use it in GitHub Desktop.
Save jplazcano87/f01a305fde64233397c45d2a6ed3a213 to your computer and use it in GitHub Desktop.
Mutate Collection inside Foreach
var test = [0, 1]
extension MutableCollection {
mutating func updateEach(_ update: (inout Element) -> Void) {
for i in indices {
update(&self[i])
}
}
}
test.updateEach {
if $0 == 1 {
$0 = 42
}
}
print(test) // [0, 42]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment