From the course: Learning Higher-Order Functions with Swift

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Foreach

Foreach

From the course: Learning Higher-Order Functions with Swift

Start my 1-month free trial

Foreach

- [Instructor] So you're already well on your way at being a higher order function pro and a Swift wizard and you're doing great. Now we're going to talk about the forEach higher order function. Now we all know what for loops are and we've also probably even seen for-in. Depends on what you want to call it, but we're going to talk about the forEach now. So we have our function names, and we're going to call forEach. Now notice what it takes. It takes a closure that takes a String, returns Void. So it doesn't return anything. It only does some operations, but has no return type. So what does it take? It takes a String and returns a Void. It doesn't return anything, so it just does some operations and it's done. It's called once for each item in the collection, in our case, four names. We can do whatever we want to to it and then it's done, just like a forEach loop. So, I'm going to hit return on this one, and notice the blue section is highlighted for the closure. And I'm just going to…

Contents