From the course: Swift 5 Essential Training

Unlock the full course today

Join today to access over 23,400 courses taught by industry experts.

Using closures

Using closures

- [Instructor] Now that we've got basic closure syntax covered, we can start working with them as parameters in functions we've already used, like sorted. For this example, we're going to create a new variable and we're going to sort our high scores array from lowest to highest. I'm going to say VAR, ascending sort, and this is going to capture our sorted array. And we're just going to say high scores dot sorted. Now you can see in the auto-complete that one of the overloaded methods accepts a closure with two integer parameters and returns a Boolean. Now don't worry about the throws and rethread is keywords we'll get to those when we talk about error handling. I'm going to select this option and I'm going to hit enter so that auto-complete can set up the closure for us leaving the local parameter names and return value for us to specify. I'm going to keep this sort of as the long hand, just for clarity. I'm going to call…

Contents