Closures in Swift - Part 2

 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 when the function starts executing the so program will exit the scope of the function before closure has finished its execution. 
We are saving the completionHandler in an array which we want to use later. So we marked closure as @escaping. 


Ways to executing the Closure: 
Storage: You can assign a closure reference and use it when needed.
Asynchronous: As you see closure passed to function stays in scope even though function gets executed from its scope. 



Non-Escaping Closures

Though @nonescaping is by default to for the closure which passed to function we will gets executed and returns back once function scope gets executed. This Synchronous in behavior. 




Note: An escaping closure that refers to self needs special consideration if self refers to an instance of a class.  A non escaping closure can refer to self implicitly.






If self is an instance of a structure or an enumeration, you can always refer to self implicitly.  However, an escaping closure can’t capture a mutable reference to self when self is an instance of a structure or an enumeration.


Structures and enumerations don’t allow shared mutability which will end up showing error as below.


Autoclosures:

Autoclosure is a closure that does not take any argument and it's wrapped the expression which passed to it.  It just returns the value of the expression which is wrapped within it,  when it's called.


let autoclosure = { print("Autoclosure") }


Note: The type of autoclosure is not String but () -> String—a function with no parameters that returns a string.


We can use @autoclosure to mark the passing closure parameter as auto closure.  We just need to pass the expression that’s all.



Wrapping:


So in this, we have completed the Closure in more detail. Hope you have understood the closure is a much better way. Closure makes code compact and shorter to write Though it bit complex and confusing to write and if you keep the practice of this it will be easier to grasp it. 

Please do comment and share the feedback!!

Reference : 


Happy Coding 😊

Comments

Popular posts from this blog

Copy Vs Mutable Copy in iOS

How to Disable iOS/iPad App availability on Mac Store, Silicon?

Functions in Swfit 3.0