Posts

Showing posts with the label closure

Closures in Swift - Part 2

Image
 As we discussed the closure in detail in the previous blog as Closure in Swift - Part 1 . I would like to suggest to go first if you want to understand this better.  Closures as Reference Types: As if we assign as  let closure =  closreReturnFromFunction()   to constant but that does not mean that its constant and closure scope can not be changed. As you have seen in the previous  Closure in Swift - Part 1 .  that counter can be modified once the closure will execute, this is because closure is reference types.   When we assigned closure to variable or constant we actually assigned closure reference not the content of closure. I Hope, it's clear !!. Escaping Closures: A closure is said to be escaping closure when it is passed as a function argument to the function and it will execute after the function returns from its scope. This is asynchronous in behavior.   Let's understand what that does It means. It's simple. Trust me. So...

Closures in Swift - Part 1

Closures: Closures in Swift are similar to blocks in C and Objective-C and to lambdas in other programming languages.  Closures can capture and store references to any constants and variables from the context in which they are defined .  Swift handles all of the memory management of capturing for you. Declaration: You can declare as below: { ( parameters ) -> return type in statements } The  parameters  in closure expression syntax can be in-out parameters, but they can’t have a default value.  A simple closure can be declared as below as well let  newClosures = {      print ( "This is new basic closure which very simple to create and call" ) } we can call using like this  newClosures (). Closure With Parameter:  we passed the parameter in the closure and can pass the string as below. The basic different closure and function is closure do not u...