📖 Scenario: Imagine you have a simple counter that you want to increase by a certain number each time you call it. You will use a closure to remember the current count and add to it.
🎯 Goal: You will create a closure that keeps track of a number and adds a given value to it each time it is called. This will show how closures in Swift are reference types because they keep their own state.
📋 What You'll Learn
Create a closure that adds a number to a stored total
Create a variable to hold the closure
Call the closure multiple times to increase the total
Print the final total to show the closure kept the state
💡 Why This Matters
🌍 Real World
Closures are used in Swift for callbacks, event handlers, and to keep state in a simple way without creating classes.
💼 Career
Understanding closures as reference types helps in writing efficient Swift code for iOS apps, especially when managing state and asynchronous tasks.
Progress0 / 4 steps
1
Create a variable total and set it to 0
Create a variable called total and set it to 0 to hold the current count.
Swift
Hint
Use var total = 0 to create the variable.
2
Create a closure addToTotal that takes an Int and adds it to total
Create a closure called addToTotal that takes an Int parameter named amount and adds it to the variable total. The closure should return the new total.
Swift
Hint
Use let addToTotal: (Int) -> Int = { amount in ... } and update total inside.
3
Call the closure addToTotal twice with values 5 and 3
Call the closure addToTotal with the value 5 and then call it again with the value 3. Store the results in variables firstCall and secondCall respectively.
Swift
Hint
Call the closure like a function: addToTotal(5) and save the result.
4
Print the values of firstCall and secondCall
Print the values of firstCall and secondCall using two separate print statements to show how the closure kept the updated total.
Swift
Hint
Use print(firstCall) and print(secondCall) to show the results.
Practice
(1/5)
1. What does it mean when we say closures are reference types in Swift?
easy
A. Multiple variables can point to the same closure instance, sharing changes.
B. Closures cannot capture variables from their surrounding context.
C. Each time you assign a closure, a new copy is created.
D. Closures are stored only as values, not references.
Solution
Step 1: Understand what reference types mean
Reference types mean multiple variables can point to the same instance in memory.
Step 2: Apply this to closures
Since closures are reference types, assigning them copies the reference, not the code itself.
Final Answer:
Multiple variables can point to the same closure instance, sharing changes. -> Option A
Quick Check:
Closures share instance references [OK]
Hint: Reference types share instances, not copies [OK]
Common Mistakes:
Thinking closures always create new copies
Confusing closures with value types like structs
Ignoring that closures capture variables by reference
2. Which of the following is the correct way to assign a closure to a variable in Swift?
easy
A. func closure = { print("Hello") }
B. let closure = { print("Hello") }
C. closure := { print("Hello") }
D. var closure = function printHello() {}
Solution
Step 1: Recall Swift closure syntax
Closures are assigned using curly braces and can be stored in variables with let or var.
Step 2: Check each option
let closure = { print("Hello") } uses correct syntax: let closure = { print("Hello") }.
Final Answer:
let closure = { print("Hello") } -> Option B
Quick Check:
Correct closure assignment syntax [OK]
Hint: Closures use curly braces and assign with let or var [OK]
Common Mistakes:
Using := which is not Swift syntax
Trying to declare closures like functions
Using func keyword incorrectly for closures
3. What will be printed by this Swift code?
var closureA = { print("Count: 0") }
var closureB = closureA
closureA = { print("Count: 1") }
closureB()
medium
A. Count: 0
B. Count: 1
C. No output, error occurs
D. Count: 0 and Count: 1
Solution
Step 1: Understand closure assignment
closureB is assigned closureA's original closure printing "Count: 0".
Step 2: Reassign closureA
closureA is changed to a new closure printing "Count: 1", but closureB still points to the old closure.