My Very First App (Guard Statements)

iDev
4 min readSep 1, 2020

For the past two weeks I have been working on my very first app, a calculator. It was a great learning experience to build this because I did everything myself. From the code in the background running all the calculations and making the UI changes, to completely laying out the UI to be clean and conform to all orientations and devices.

So earlier in the week before the calculator assignment, I learned the “guard” statement in the Swift language. This is a key word that helps you perform a certain task. The best way to describe it is that it prevents you from having to execute a bunch of code if the guard statement is false. It will stop (lets say a function) from being redundant with “if” statements. Here’s a pic of the syntax:

It checks for a false statement first, and if the condition is false, the code between the brackets after the “else” statement is executed. Putting the word “return” in those brackets basically escapes the function and stops the function from continuing. The following is something I just made up, to show you a function that could use a guard statement.

This is a function that gives you a message that tells you how many even numbers there are between two given numbers; a start and an end. Now, imagine you had some large numbers to put into the function. What would happen if you put the numbers in incorrectly and the start number is larger than the end number? You wouldn’t get the result you wanted. So, to prevent this code from running through all those calculations, you could use a “guard” statement to prevent the unnecessary calculations of the function if the start number is more than the end number. Below would be a solution.

Remember, the guard statement checks for a false result. So the guard statement here says “if the start number being less than end number turns out to be false, return this statement instead and do not complete all the code below.” I used these guard statements a couple of times in my calculator project. I was surprised how much I could do on my own and I took away a lot.

One thing I really took away was how much logic goes into such a simple app. I gained a new respect for even the simplest of things in iOS Development. There is a lot behind the UI that we don’t see. When I wanted to add a little feature like deleting the last inputed number, it was a challenge for just one feature. I took it upon myself to complete the optional more difficult features and I’m glad I did. Here’s a look at my first calculator app.

Simple and clean was what I was going for. It works in landscape orientation and from the smallest iPhone to the largest iPad. My peers seemed to like it so I was satisfied.

Being satisfied was something I struggled with. I was such a perfectionist that I was trying to do everything and it was holding me back. After talking to some of the guys doing the academy too, it was more important to have it finished and completed rather than having all the functionality I could think of. No, it’s not perfect under the hood and it could use some work, but for the most part it does what you need it to and now I can say I’ve built an app from start to finish!

--

--