Programming in Swift: Functions & Types

Jan 4 2022 Swift 5.5, iOS 15, Xcode 13

Part 2: Closures

10. Closures

Lesson Complete

Play Next Lesson
Next
Save for later
About this episode
See versions

See course reviews

See forum comments
Cinema mode Mark as Complete Download course materials
Previous episode: 9. Introduction Next episode: 11. Challenge: Closures

Update Notes: This course was originally recorded in 2019. It has been reviewed and all content and materials updated as of October 2021.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.

As I said, closures and functions are really just different flavors of the same thing. Actually, technically, functions are a special type of closure!

var operate = add
add(number1: 7, number2: 3)
operate(7, 3)
var addClosure
var addClosure😺: (Int, Int) -> Int
var addClosure: 😺Operate
var addClosure: Operate 😺= {

}
var addClosure: Operate = { 😺(a: Int, b: Int)
var addClosure: Operate = { (a: Int, b: Int) 😺-> Int
var addClosure: Operate = { (a: Int, b: Int) -> Int 😺in
return a + b
❌return❌ a + b
var addClosure❌: Operate❌ = 
addClosure(7, 3)
var addClosure = { (😺num1 a: Int, 😺num2 b: Int) -> Int in
var addClosure = { (a: Int, b: Int😺 = 10🛑) -> Int in
printResult(add, 4, 5)
printResult(operate, 4, 5)
printResult(addClosure, 4, 5)
printResult(+, 4, 5)
printResult(operate: (Int, Int) -> Int, a: Int, b: Int)
printResult(
  { 

  }...
  (a: Int, b: Int)
  (a: Int, b: Int) 😺-> Int
  (a: Int, b: Int) -> Int 😺in
    a * b  + 100
    a * b + 100
  }, 
  3, 10
)