DEV Community

Cover image for Closure (Concept)
DanielJang
DanielJang

Posted on

Closure (Concept)

Closure is very important basic concept in Swift.
And many iOS API are using closure syntax.
Let's see Closure for basic concept.

Definition

  • Closure is function without name (Anonymous function)
  • Closure can be passed / used to argument of function

Syntax

{ (Parameters) -> (ReturnType) in Statements } //Example { (a: Int, b: Int) -> (Int) in a + b } 
Enter fullscreen mode Exit fullscreen mode


Function vs Closure

Function has “Name” ——— Closure doesn't

func myFunction(parameter1, prameter2) -> (ReturnType) { Statements } //Step 1. Remove "func" Keyword and "function name" (parameter1, prameter2) -> (ReturnType) { Statements } //Step 2. Move all to curly brace with "in" keyword { (parameter1, prameter2) -> (ReturnType) in Statements } 
Enter fullscreen mode Exit fullscreen mode

Function has “Argument labels” ——— Closure doesn’t (But can have parameter names)

func myFunction(argumentLabel parmeter:) //but Closure cannot use argumentLabel as above 
Enter fullscreen mode Exit fullscreen mode

Function has “Default parameter value” ——— Closure doesn’t

//myFunction and default value for parameter2 //but Closure cannot have defualt value for parameters func myFunction(parmeter1: Int, parmeter2: Int = 0) 
Enter fullscreen mode Exit fullscreen mode

But Closure can be used “Inline"

//first parameter addValue is closure format(anonymous function) func addTwoInteger(addValue: (a: Int, b:Int)->Int, a: Int, b: Int) { print(addValue) } //Example addTwoInteger( { (a: Int, b: Int) -> Int in a + b }, //iniline closure 3, 2 ) 
Enter fullscreen mode Exit fullscreen mode


Short Syntax - Let’s make Closure more simple !!!

//we declare custom type which is closure typealias OperateType = (a: Int, b:Int) -> Int 
Enter fullscreen mode Exit fullscreen mode

Step1. Basic syntax

let myClosure = { (a: Int, b: Int) -> Int in a + b } 
Enter fullscreen mode Exit fullscreen mode

Step2. Declare variable explicitly then compiler could know "a" is Int, "b" is Int and "return type" is Int also. So we can remove data types "Int" from parameters and return type

let myClosure: OperateType = { (a, b) in a + b } 
Enter fullscreen mode Exit fullscreen mode

Step3. Also compiler could know "a" and "b" parameter from definition of OperateType too. So remove "(a, b)" and "in" keyword

let myClosure: OperateType = { a + b } 
Enter fullscreen mode Exit fullscreen mode

Step4. Based on Swift common syntax, we can use $0 instead first parmeter "a" and $1 instead second parmeter "b"

let myClosure: OperateType = { $0 + $1 } 
Enter fullscreen mode Exit fullscreen mode

Call Closure

myClosure(3, 4) 
Enter fullscreen mode Exit fullscreen mode


What is trailing Closure ? - When Closure is located as last parameter of function !

// See below function, myClosure is "last parmetter" func myFunction(a: Int, b: Int, myClosure: (a: Int, b:Int) -> Int){ print(myClosure) } //You can call as below myFunction(3, 4, { a, b in a + b } ) //or call by simple sytax myFunction(3, 4, { $0 + $1 } ) //When closure is placed as last parmeter, we can pull out after close parentheses myFunction(3,4){ $0 + $1 } 
Enter fullscreen mode Exit fullscreen mode


Yeah~~~ Done !!!

Top comments (0)