You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
letdriving={print("I'm driving in my car")}driving()
Accepting parameters in a closure
Type functionName = { (parameter: Type) in ... }
letdriving1={(place:String)inprint("I'm going to \(place) in my car")}driving1("London")
Returning values from a closure
parameter와 in 사이에 -> Type을 넣어줌.
return returnValue
letdriving2={(place:String)inprint("I'm going to \(place) in my car")}letdrivingWithReturn={(place:String)->Stringinreturn"I'm going to \(place) in my car"}letmessage=drivingWithReturn("London")print(message)
Closures as parameters
closure를 일급 객체( 여기서는 parameter)로 사용할 수 있음
letdriving3={print("I'm getting ready to go.")}func travel(action:()->Void){print("I'm getting ready to go.")action()print("I arrived!")}travel(action: driving3)
Trailing closures syntax
func travel1(action:()->Void){print("I'm getting ready to go.")action()print("I arrived!")}travel1(){print("I'm driving in my car")}travel{print("I'm driving in my car")}