Skip to content

Instantly share code, notes, and snippets.

@soffes
Created July 17, 2014 19:08
Show Gist options
  • Save soffes/f140254c41b633e38e69 to your computer and use it in GitHub Desktop.
Save soffes/f140254c41b633e38e69 to your computer and use it in GitHub Desktop.
Each on Array in Swift
extension Array {
func each(block: ((Element) -> Void)) {
for i in 0..<count {
block(self[i])
}
}
func eachWithIndex(block: ((Element, Int) -> Void)) {
for i in 0..<count {
block(self[i], i)
}
}
}
@subdigital
Copy link

You can also do

for (index, item) in enumerate(array) {
   ...
}

@soffes
Copy link
Author

soffes commented Jul 17, 2014

Oh, that is nicer. Good call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment