Attention is Obvious

What has your attention today? Maybe you’re thinking about your love. Or how you don’t want to be at work. Perhaps you are time-traveling back to the conversation you had yesterday. Whatever has your…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




What is Defer keyword in Swift?

This article revolves around one of the most famous interview topic “defer in iOS”. We will not only know what exactly is defer but also will concrete our concept on defer with help of real interview questions.

Swift’s defer keyword lets us set up some work to be performed when the current scope exits.

A defer statement is used for executing code just before transferring program control outside of the scope that the statement appears in.

According to the official documentation by Apple “A defer statement is used for executing code just before transferring program control outside of the

scope that the defer

statement appears in.”

Let’s try to understand the statement with help of examples cum interview questions.

Q.1)Try to find what would be the output of the following piece of code

From the definition of defer we are aware that defer will run just before transferring the program control and in the above code snippet the program control will exit only when return value (at line 8) statement is encountered. Lets see how the program execution will happen line by line, firstly when program control will reach at line number 10 then the flow will quickly move to line 4 and now when the control will move inside the function then line 5 will be encountered but the program flow won’t move inside that block (since it is defer and defer runs at end i.e before exiting ) and thus the control will move to line 8 thereby returning value as 0 and which will be printed but by that time defer block will be invoked and thus line 6 will be executed which result in increasing the value to 1. Now when in line 11 we are printing the value we will get 1 since the value has then been increased by 1(at line 6).

Output:

Q2.)Predict the output of the following piece of code

Looking closely into the above code snippet we can make out that when the program control will come at line 2 it will simply print Step 1 now the control will move to line 3 but won’t get inside it since it is a defer statement and it will run only when the control will try to move out of the interViewQuestion() and so the control will move to line 7 thereby printing Step 3 now the control will try to move out of interViewQuestion() and therefore the defer statement will be called and thus Step 2 will be printed.

Output:

Q3.) Predict the output of the following piece of code

Earlier in question 2 it was quite straight forward since there was only one defer statement but herein this question we can find multiple defer. There is one thumb rule while dealing with multiple defer and that is- at the time of execution the execution will take place in a reverse order as they the defer are declared in the program i.e bottom to top. So let’s try to break the above piece of code the same way i.e line by line. Firstly the program control will reach at line 2(after being invoked from line 14) and Step 1 will be printed. Now the control won’t move inside line 3,6 and 9 and so the control will move to line 12 thereby printing Step3 and now comes the turn for defer present at line 3,6 and 9 and as we know the execution will happen just bottom to top, therefore firstly differ of line 9 will be executed and then the defer of line 6 and finally of line 3 thereby resulting in the following:

Q.4.) Predict the output of the following piece of code

This is one of the most typical question on defer that a candidate can be checked. In the above code snippet the fundamental is same and which is firstly Step 1 will be printed and then Step 3 and now comes the turn for defer and as we saw in question 3 firstly defer of line 12 will be executed thereby printing Step5 now comes the tricky part which is defer of line 6, to understand thus let’s suppose we were given following piece code

So clearly we can say that firstly Step4 will be printed and then Step 6 so similar thing will happen to defer of line 6 in previous code snippet and therefore Step4 will be printed followed by Step 6 and lastly Step2 will get printed thereby resulting the output screen as

So this was all about the defer in iOS.

Enjoying!! I will be adding more post like this. Follow me so that you don’t miss any of them.

If you enjoyed reading this post, please share and give some claps so others can find it 👏👏👏👏👏 !!!!

If you have any comment, question, or recommendation, feel free to post them in the comment section below!

Add a comment

Related posts:

How to Eat Breakfast

Your alarm goes off at 5. You pull on your running clothes in the dark, press go on your Garmin, and make it back in time for a shower, coffee, and your first meeting. Suddenly it’s lunchtime and…

Do Androids Dream of Criterion Streams?

I have not been to the movie theater in over a fucking year. This is a bummer. Before the pandemic, my kids and I would go at least two or three times a month. We’d see whatever — well, typically “R”…

Is It Worth Investing in Search Engine Marketing Services?

Selling online products and services is getting difficult day by day because of the rapidly increasing competition. In short, utilizing appropriate marketing strategies is now the only way to keep…