DEV Community

Oluwasanmi Aderibigbe
Oluwasanmi Aderibigbe

Posted on

Day 7 of 100 days of SwiftUI

Day 7 of HackingWithSwift's 100 days of SwiftUI. Today I learnt about to use using closures as parameters when they accept parameters, returning a closure from a function, and capturing values in a closure.

In swift returning a closure from a function is written like this.

func returnClosure() -> (name: String) -> Void {
     return {
       print("Hello world \(name)")
     }
}

let action = returnClosure()
action("sanmi")
Enter fullscreen mode Exit fullscreen mode

The code above creates a function that returns a closure. The first -> is used to signify the return type of the function while the second -> is used to signify the return type of the closure.

Capturing values in a closure means any external variable that is used inside of closure is stored alongside the closure and can be modified even if they don't exist anymore.

If you are interested in taking the challenge, you can find it at https://www.hackingwithswift.com/100/swiftui
See you tomorrow ;)

Top comments (0)