Using closures with Swift's array algorithms
In Chapter 3, Using Collections and Cocoa Data Types, we looked at several built-in algorithms that we could use with Swift's arrays. In that chapter, we briefly showed how to add simple rules to each of these algorithms with very basic closures. Now that we have a better understanding of closures, let's see how we can expand on these algorithms using more advanced closures.
In this section, we will primarily be using the map algorithm for consistency purposes; however, we can use the basic ideas demonstrated with any of the algorithms. We will start by defining an array to use:
var guests = ["Jon", "Kim", "Kailey", "Kara"]
This array contains a list of names and the array is named guests
. This array will be used for all examples in this section except for the very last ones.
Now that we have our guests
array, let's add a closure that will print a greeting to each of the names in the guests
array:
guests.map({ (name: String) -> Void in println...