Created
August 16, 2018 13:25
-
-
Save jplazcano87/f01a305fde64233397c45d2a6ed3a213 to your computer and use it in GitHub Desktop.
Mutate Collection inside Foreach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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