Calculator Codes
Start course
2h 50m

This course will provide you with a comprehensive understanding of the fundamentals of Swift. We're going to learn about variables, constants, arrays, dictionaries, sets, if statements, and more! 

Intended Audience

This course is designed for anyone who wants to:

  • Learn about iOS development and coding
  • Move into a career as an iOS developer
  • Master Swift skills


To get the most out of this course, you should have some basic knowledge of iOS.


Hi. Within this lecture, we're going to define the views into our view controller and then we're going to work on them. So, let me add a new editor over here and you can click on this ViewController to bring View Controller on the right-hand side, or remember, you can choose the ViewController.swift from here as well. So, this is the same thing. And in the left-hand side, we already see the main storyboard. So, all we have to do is to click drag and drop the view. So, remember you have to press ctrl in your keyboard and then drag and drop inside of the class but outside of this function. So, inside of this class, I'm going to call this firstNumber or firstText number. 

I'm going to call this firstText. I believe this is the simplest thing that I can call. So, this is firstText. Let me add some couple of more lines and I'm going to bring in the second text. So, this is the secondText. So, let's say Connect. And then, I believe we have to bring our label and then our buttons as well. Remember, if you made a mistake, you can break the connections over here and delete everything and redefine. But if not, just don't go with it. So, I'm going to need some more actions to connect to over here but first, I'm going to get my result label. So, let me bring this over here and call this resultLabel. So we are done with the text fields and labels.

Now, let's go for actions. So, first will be my adding summation function. So, let me try to take this and bring this over here, and this is going to be an action. And let's call this something like sumAaction or sumButtonClicked. Let's call it sumClicked and connect to our View Controller. So, we have our first action. Let's go for others. So, the second one will be deduction. So, I'm going to call this minusClicked. I'm not being consistent over here. One is sumClicked and the second one is minusClicked. Maybe I could have called it deductionClicked or something but it doesn't matter. So, third one will be multiplyClicked. And we are here for the last one. So, let's go for the last one and drag and drop to our View Controller. So, I'm going to call this divideClicked. So, let's call it divideClicked and then connect. So, here we go. We have defined everything in our view controller. So, we are now ready to go and write our code. 

So, let me close this down and let me choose View Controller and run this on my simulator to actually see how it looks like on a real phone or a real simulator screen. It's wise to do that at this point because if you see any problem without going through all of the changes in the coding section, you may want to adjust the views a little bit and then continue writing your code. So, let me bring this simulator up and see if we can get the results over here. And maybe we can just take a look at the simulator settings as well because we have seen how to run the simulator but we haven't seen the settings of the simulator here as well because simulators have their own settings. And when you click on a simulator, you can reach their menu. So, these are two different kinds of software and they have their own menus. So, you can access them from here. So, we see our app. We click on first number for example, and we can try to write something. It will write. So, everything seems to be in order.

And let me show you what I mean by settings. If you go to Debug section over here, for example, you can see some location or if you go to hardware, you can see the keyboard. So, maybe you can just say Toggle Software Keyboard and it will pop up the keyboard for you. So, you can type it from here as well. Generally, developers don't like to use it that way, so they can type it from their own keyboard but you can change the settings from this keyboard menu as well. So, you can do whatever you want with this simulator. You can change its location. You can change its name. So, this is a great tool and as you can see, nothing happens as I click on these buttons because I haven't written them yet. So, let's stop this and go to ViewController.swift in which where we're going to write our codes. So, we're going to start with sumClicked. 

We're going to test the first function and if that worked, we're going to reapply the same techniques to other functions as well. So, what do I want to do in the sumClicked? First, I want to get the first number. And then I want to get the second number and I want to display the result of their summation together in the result label. So, let me create first number variable. So, the first number will be firstText. So, this will be the place. This will be the view that our user is going to be writing their inputs. So, I'm going to say firstText.text. So, if you and just look at the attributes and methods over here, you will see something like text. So, this is the text display by the text field. So, whatever has our user written in this text field, we can get it by saying firstText.text. So, this is cool. We already got what is written inside of the first text but this is going to be an string. It's not going to be an integer. So, we just got this firstText.text, and it doesn't make sense to add this string to another. We have to convert this into an integer or a double in order to make the same thing with the secondText and they sum it up together. So, wrap this around an integer and this will convert this text into an integer. What is the problem here? So, we have to force cast this or we have to provide a ?? in order to make this optional and provide a default value as you might remember. So, I'm going to Force-unwrap this first. I will try and see if that works with force-unwrapping. If that works, very good. If that doesn't, I will change it.

I'm going to do the same thing for a second number as well. So, I'm going to say let secondNumber is going to be the integer of secondText.text. Int(secondText.text!). So, that's okay. And we're going to sum these two together. And we still haven't done yet so we have to force-unwrap this one more time. I'm going to show you why. I'm going to create another value over here. I'm going to call this result. This is going to be the integer. This is going to be firstNumber plus secondNumber. So, this is first number but we see this is integer optional. So, this isn't good. So, secondNumber is integer optional as well. And guess what? We cannot do that because the binary operator of summation, binary operator of summing these two numbers together cannot be applied to 'Int?'. So, we have force-unwrapped this. Why it is an optional? Because right now, the first exclamation point only stands for firstText.text, is going to be a text. We are certain of that. And if we put another exclamation point over here, it means that this is definitely going to be an integer. So, as you can see now this doesn't give me any error because the first number is not an integer optional anymore. So, let me tell you one more time.

The first exclamation point means that this firstText.text is going to be definitely over there and we know that even if user doesn't give an input, we're going to get a string from there, an empty string. We're not going to get a nil value. We are certain of that. So, this is over here. We are certain that firstText is defined and it's not going to return as an nil value. It's going to, at first, It's going to return as an empty string and we're making certain of that by putting this exclamation point in this stage. And if we add the other one, if we add the second one, it means that we are certain that we're definitely going to convert it into an integer. Let's see if we delete this and if I come over here and if I write first number, it will make this integer an optional integer. And this is okay for this is optional. But if I put this, this is going to be an not optional. This is going to be a regular integer. And by this time, we are bedding. 

We're saying that this is definitely going to be a string and this string is definitely going to be converted into integer. And later on, I can just take this result label and I can say resultLabel.text = result. But I cannot say that because resultLabel.text is expecting a string from me and I'm giving an integer to it. So, I have to convert this into a string. So, I can just go like this: String (result). So, this will convert result into a string and I can definitely do that once I got the result. I can definitely make an integer into a text. This is going to be okay anyway. So, let me run this and let's see if this works. So, we're going to get the firstText. So, let me give this something like 40 or 5. So, let me give this 10. Let me just sum them together and here we go. They're 15. So, let me give -10 and -5. So, let me give a big number and let me give a very big number so that's still working and it doesn't seem to be crashing. 

But if I come over here and say Apple, and if I want to sum them together, as you can see, it crashed. So, this is definitely going to be this way because as you can see it says that it is unexpectedly found nil while unwrapping an optional. So, we have said that this is definitely going to be over there but it's not there. We couldn't even convert this to be an integer. So, since this integer is not there, since this integer is a nil, the application crashed. And the good thing is you know how to make this safe. You know what to do in a situation like this. Of course, you can go with ?? or you can go with default values but we do not want that. We just want to have a safe number and have a safe calculation so that even if the user doesn't give the right input, we would not crash the app and we would get the result that we want. We're going to do that in the next lecture.


About the Author
Learning Paths

Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.

Covered Topics