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 - Swift Tutorial
From the course: Swift 5 Essential Training
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
(Locked)
Leveling the playing field30s
-
(Locked)
Basic functions3m 25s
-
(Locked)
Overloading functions3m 12s
-
Complex functions5m 9s
-
(Locked)
Function types3m 25s
-
(Locked)
Understanding closures4m 43s
-
(Locked)
Using closures7m 38s
-
(Locked)
Type aliasing4m 2s
-
(Locked)
Challenge: Battlegrounds37s
-
(Locked)
Solution: Battlegrounds5m 50s
-
(Locked)
-
-
-