The closures in Swift are explained with examples

Swift Closures

Posted on 11 Apr 2021 . 3min read


Closures are a difficult topic to grasp in Swift, but they are simple to grasp if well described. Closures are functional blocks that can be used or passed around in code.


Closures are an old term that is identical to C and Objective-C blocks. Closures are similar to functions, but they are more flexible and have a more streamlined appearance. Closures are nested functions, and they can capture value, as we'll see later in this article. Closures have the following significant characteristics:

  • It can be assign to variables or constants
  • It can be added to arrays, dictionaries or sets, among other things.
  • Closures can return from other functions or closures
  • Receive as function and closure parameters


Syntax


The following is a very clear and clean syntax for closures:

{(parameters) -> return type 
     in
     statements
}


Assign closures to variables or constants


let a = {(b:Int) -> Int
    in
    return b * 5
}


Tuples can be used as parameters in closures, and they can't have a default value. The closure accepts an integer parameter and returns an integer that has been multiplied by 5.


Closures in an array or other collection types


let closures = [
    {(a: Int) -> Int in return a * 3}
]


Let's start with a simple array with only one closure that takes an integer as a parameter and returns an integer multiplied by three.


Inferring Type


Proceed to closure with a number of closures that all take an integer as a parameter and return an integer. We don't need to rewrite it because Swift can infer the types of the parameters and values it returns.


let closures = [
    {(x:Int) -> Int in return x * 7},
    {x in return x - 2},
    {$0 * 4}
]


The first closure in the preceding example has one integer parameter and returns an integer value. Since Swift infers type, we don't need to write the type of parameters and return values in the second closure. Finally, we have closure argument names in shorthand.


Variable capture


A closure can catch constants and variables specified in the body, as well as change their value. Since functions and closures are the same thing, nested functions are just another type of closure.


func counterMaker(number: Int) -> Int {
    var total = 0
    //function within function
    func addNumber() -> Int {
        total += number
        return total
    }
    return addNumber
}


There is a function called counterMaker in the example above that takes a number of type integer as a parameter and returns an integer value. It has an inner function called addNumber that captures two values: total and number, and counterMaker returns addNumber as a closure after capturing the values. Every time it calls, it adds a number to the total.


The bottom line


Since functions and closures are the same thing, use what syntax makes the most sense at the moment. It is entirely up to you to decide which one you want at any given time.


Don’t hesitate to contact me if you have any questions or queries. Follow me on twitter @gurjitpt for any updates.

Thanks!


Share this article



Written By

Generic placeholder image

Gurjit Singh

I’m Computer Science graduate and CompTIA Security+ certified SOC Analyst and Mobile Application Security Engineer with 10+ years of cross-platform development experience across iOS, Android, and web.


Discover articles by topics

SwiftUI Class Struct Networking XCode NSCache Enum Optionals Property Observers Closures Guard Reviews StoreKit App Store Algorithms Testing Operators Protocol Extensions Weak Unowned SwiftData WWDC23 GCD API Admob SwiftLint Lottie Foreach Objective-C UIKit NavigationSplitView

Related Articles


MobSF Audit of an iOS app powered by the Flickr API

Static analysis of an iOS app using MobSF, identifying credential misuse, security misconfigurations, and privacy issues....

2026-05-29 . 4 min read     MobSF SAST

Read More »

Understanding Certificates in Cybersecurity

Transitioning from software development into cybersecurity is one of the most natural career moves in tech today....

2026-05-26 . 4 min read     Security Certificates

Read More »

Swift 6.1: New Features & Enhancements

Swift 6.1, officially released in March 2025, continues the evolution of Apple's powerful and expressive programming language....

2025-08-12 . 3 min read     Swift 6.1

Read More »

String Concatenation in Swift: A Comprehensive Guide

In any programming language, working with strings is essential, and Swift is no different.Whether you are building iOS apps......

2024-10-17 . 3 min read     String Concatenation

Read More »

Integrating SwiftUI with UIKit Using UIHostingController

With the introduction of SwiftUI, Apple has provided developers with a modern way to build user interfaces across all Apple platforms....

2024-07-09 . 3 min read     UIHostingController

Read More »

Deep Dive into Autorelease Pools in Swift

In the realm of software development, memory management plays a crucial role in ensuring the efficient allocation and deallocation of memory...

2024-01-28 . 4 min read     Swift Autorelease

Read More »