0
0
iOS Swiftmobile~20 mins

Functions and closures in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Closure Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a closure capturing a variable
What will be printed when this Swift code runs?
iOS Swift
func makeIncrementer(amount: Int) -> () -> Int {
  var total = 0
  let incrementer: () -> Int = {
    total += amount
    return total
  }
  return incrementer
}

let incrementByTen = makeIncrementer(amount: 10)
print(incrementByTen())
print(incrementByTen())
A10\n20
B0\n10
C10\n10
D20\n30
Attempts:
2 left
💡 Hint
Closures can capture and modify variables from their surrounding context.
ui_behavior
intermediate
1:30remaining
Closure as a button action handler
In SwiftUI, what happens when this button is tapped?
iOS Swift
struct ContentView: View {
  @State private var count = 0
  var body: some View {
    Button(action: {
      count += 1
    }) {
      Text("Tap count: \(count)")
    }
  }
}
AThe button label updates to show the number of taps.
BThe button does nothing when tapped.
CThe app crashes due to state mutation in closure.
DThe button label shows a fixed number 0 always.
Attempts:
2 left
💡 Hint
State variables update the UI when changed inside closures.
lifecycle
advanced
2:30remaining
Memory management with closures
What problem can occur with this Swift code involving closures?
iOS Swift
class ViewController {
  var completionHandler: (() -> Void)?
  func setup() {
    completionHandler = {
      print(self)
    }
  }
}
ANo problem; closure captures 'self' weakly by default.
BClosure runs before 'self' is initialized causing crash.
CStrong reference cycle causing memory leak.
DCompilation error due to missing capture list.
Attempts:
2 left
💡 Hint
Closures capture variables strongly unless specified otherwise.
📝 Syntax
advanced
1:30remaining
Correct syntax for a closure with parameters and return value
Which option correctly defines a closure that takes two Ints and returns their sum?
Alet sum = { (a, b) -> Int in a + b }
Blet sum = { (a: Int, b: Int) -> Int in return a + b }
Clet sum = { a, b in a + b }
Dlet sum = (a: Int, b: Int) -> Int { a + b }
Attempts:
2 left
💡 Hint
Closures with parameters and return types need 'in' keyword separating parameters and body.
🔧 Debug
expert
3:00remaining
Why does this closure cause a crash?
This Swift code crashes at runtime. What is the cause?
iOS Swift
class Example {
  var closure: (() -> Void)?
  func configure() {
    closure = { [unowned self] in
      print(self.description)
    }
  }
}

var example: Example? = Example()
example?.configure()
let capturedClosure = example!.closure!
example = nil
capturedClosure()
ANo crash; code runs and prints description.
BClosure is nil so calling it crashes.
CUnowned capture causes compile-time error.
DAccessing 'self' after it was deallocated causes a crash.
Attempts:
2 left
💡 Hint
Unowned references do not keep the object alive and cause crash if accessed after deallocation.