DEV Community

loading...
Cover image for Day 6 of 100 Days of SwiftUI

Day 6 of 100 Days of SwiftUI

ramsescabello profile image Ramsés Cabello⚡️ ・1 min read

Day 6

Definitely "Closures" are the most difficult bit to understand until now. It is quite abstract and can't still figure out how I will be using them in the future.

Some initial notes about Closures:

Closures

Closures are functions assigned to variables (?). The syntax is written as:

let closureName = {
    code
}
Enter fullscreen mode Exit fullscreen mode

They can be assigned parameters if those are placed between parameters:

let closureName = { (parameter: DataType) in
    code
}
Enter fullscreen mode Exit fullscreen mode

Closures can return values as well:

let closureName = { (parameter: DataType) -> DataType in
    return code
}
Enter fullscreen mode Exit fullscreen mode

Trailing closure syntax

If the last parameter of a function is a closure, Swift lets you use special syntax called trailing closure syntax:

function() {
    code
}
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

pic
Editor guide