Programming in Swift: Functions & Types

Jan 4 2022 · Swift 5.5, iOS 15, Xcode 13

Part 2: Closures

10. Closures

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 09. Introduction Next episode: 11. Challenge: Closures

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 10. 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’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

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
)