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
Using closures as parameters when they accept parameters
// First, We need a function with parameter
func tryDriveCar (tryDrive:(String)->Void){print("Drive")tryDrive("Drive car")print("Car")}
// Now, the queue is the how-to run this
tryDriveCar{(doIt:String)inprint("I can \(doIt)")}
// Drive
// I can Drive car
// Car
Using closures as parameters when they return values
// First, We need a function with parameter
func tryDriveCar (tryDrive:(String)->String){print("Drive")lettryCar=tryDrive("Drive car")print(tryCar)print("Car")}
// Now, the queue is the how-to run this
tryDriveCar{(doIt:String)->Stringinreturn"I can \(doIt)"}
// Drive
// I can Drive car
// Car
Shorthand parameter names
// Let's continue by shortening the above function
func tryDriveCar (tryDrive:(String)->String){print("Drive")letdrive=tryDrive("Drive Car")print(drive)print("Car")}tryDriveCar{(doIt:String)->Stringinreturn"I can \(doIt)"}
// Now let's see how to write it shorter
tryDriveCar{ doIt ->Stringinreturn"I can \(doIt)"}
// One more. Because Swift know this closure must be return string
tryDriveCar(){ doIt inreturn"I can \(doIt)"}
// One more. Because Swift know this closure must be return
tryDriveCar(){ doIt in"I can \(doIt)"}
// And last one. Since the parameters are sequential, we can call the first parameter 0.
tryDriveCar{"What's going on. \($0)"}
// Every result is the same but code readability... Not sure about that.
Closures with multiple parameters
// Okay lets try with multiple parameter
func tryDriveCar (tryDrive:(String,Int)->String){print("Drive")lettryCar=tryDrive("Drive car",22)print(tryCar)print("Car")}tryDriveCar{"I can \($0)\($1) Model BMW"}
// I also wrote another way. I think this type is best
tryDriveCar{(myText, myModel)in"\(myText)\(myModel)"}
// I can Drive car 22 Model BMW
// Drive
// Drive car 22
// Car
Returning closures from functions
func clousureFunc()->(String)->Void{ // This syntax is too confusing. But the first arrow is returned by our function.
return{ myText inprint("clousure parameter is : \(myText)")}}
// How to use this?
letcallFunc=clousureFunc()callFunc("Tried")
// clousure parameter is : Tried
// Or. As you wish
letcallFunc2=clousureFunc()("Tried 2")
// clousure parameter is : Tried 2
Capturing values
// This is so easy.
varfuncCount=0func clousureFunc()->(String)->Void{varcount=0
funcCount +=1print("Func called \(funcCount) times")return{ myText in
count +=1print("clousure parameter is : \(myText) and count : \(count)")}}letresult=clousureFunc()result("One")result("Two")result("Three")
// Func called 1 times
// clousure parameter is : One and count : 1
// clousure parameter is : Two and count : 2
// clousure parameter is : Three and count : 3
// If I call the function second time
letresult2=clousureFunc()
// It is added to the output.
// Func called 2 times