Store a closure as a variable in Swift
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Swift, a closure is just a value of function type, which means you can store it in a variable, pass it around, and return it from another function. The important part is declaring the closure's parameter and return types clearly enough that the compiler and future readers know what the variable is supposed to do.
Store a simple closure in a variable
A closure variable is declared with a function type such as (Int) -> Int or (String) -> Void. Then you assign a closure expression to it.
The variable doubleValue stores a closure that accepts one Int and returns one Int. Because the type is explicit, the closure body can stay compact.
Use var when the closure needs to change
If the closure is a configurable callback, declare it with var so a different closure can be assigned later.
This pattern is common for strategy-style behavior, test doubles, and UI callbacks where the implementation changes based on state.
Closures can capture surrounding values
A stored closure can capture constants and variables from the scope where it was created. That is one of the reasons closures are so useful, but it is also why they need careful ownership handling in reference types.
The returned closure still knows base even after makeAdder has finished. That captured state is part of the closure value.
Storing closures as properties
Closures are often stored as properties on structs and classes. This is common in view models, completion handlers, and dependency injection.
The double parentheses in ((String) -> Void)? mean the property is an optional closure. That is a common and useful pattern when the callback may or may not be assigned.
Be careful about retain cycles in classes
When a class stores a closure and that closure captures the same instance strongly, you can create a retain cycle. That is one of the most important practical issues with stored closures in Swift.
The capture list [weak self] prevents the closure from keeping the object alive forever. Not every stored closure needs this, but class-owned callbacks often do.
Type aliases make closure variables easier to read
If the signature is long, use a type alias. That makes stored closures more maintainable and reduces repetition.
This is especially helpful in APIs with several related callback types.
Common Pitfalls
- Forgetting to declare the closure type clearly, which makes the code harder to read.
- Using
letwhen the closure needs to be replaced later. - Storing a closure on a class and capturing
selfstrongly, creating a retain cycle. - Overusing optional closures when a required dependency would be clearer.
- Writing overly complex closure bodies when a named method would communicate intent better.
Summary
- In Swift, closures are values and can be stored in variables or properties.
- Declare the closure type explicitly for clarity.
- Use
varwhen the closure should be replaceable. - Stored closures can capture surrounding values and state.
- In classes, watch for retain cycles and use capture lists when needed.
Related reading
- Storing authentication tokens on iOS - NSUserDefaults vs Keychain?
- Storyboard - refer to ViewController in AppDelegate
- Storyboard doesn't contain a view controller with identifier
- Storyboard Segue From View Controller to Itself
- Storyboard warning prototype table cells must have reuse identifiers
- Streaming video from Android camera to server
- String Resource new line /n not possible?
- String search in string array in objective c
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.