Posts

Showing posts with the label Closures

Closures in Swift

Closures in Swift Closures in simple terms is a block of code which can be called any time during the current flow of execution. Closures have access to variables and function in the scope where they are defined. We can consider a simple array of numbers and try to transform it into a map with closure. let numbers = [1,2,3] The map function considers input a closure , will start with most extensive version of it as shown below. Iteration 1----------------------------------------------- let newMap = numbers . map ({ (input: Int ) -> Array < Int > in     return Array (repeating:input, count: input) }) "in" is used to separate the arguments and return type from the body. Iteration 2----------------------------------------------- If we see the Closure is the only parameter of the function . We can omit parentheses. We just removed () . let newMap2 = numbers . map { (input: Int ) -> Array < Int > in   ...