‫Instructor: Hi.
‫Within this lecture, we are 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 view controller
‫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 click drag and drop the view.
‫So, remember you have to press control 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 "firstTextNumber", okay?
‫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 "secondText."
‫So this is "secondText."
‫Okay?
‫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 resultLabel.
‫So let me bring this over here and call this "resultLabel."
‫Okay, so we are done with the text fields 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 "someAction"
‫or "someButtonClicked", okay?
‫Let's call it "someClicked"
‫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 "someClicked" 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."
‫Okay?
‫And we are here for the last one.
‫Okay?
‫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 "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, okay?
‫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 result over here.
‫And maybe we can just take a look
‫at the simulator's headings 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 they,
‫simulators have their own settings,
‫and then you click on a simulator, you can reach their menu.
‫Okay?
‫So these are two different kind of softwares
‫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.
‫Yeah, it'll 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, okay?
‫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 are going to write our codes.
‫So we are going to start with the sumClicked.
‫We are going to test the first function and if that works,
‫we're going to reapply the same techniques
‫to other functions as well.
‫So what do I wanna do in the sumClicked?
‫First I want to get the first number, right,
‫and then I want to get the secondNumber,
‫and I want to display the result
‫of their summation together in the resultLabel.
‫So let me create firstNumber variable.
‫So the first number will be firstText, right?
‫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 just look at the attributes and methods over here,
‫you will see something like text.
‫So this is the text displayed 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 gonna be an integer.
‫Okay?
‫So we just got this "firstText.text",
‫and it doesn't make sense to add this string to another.
‫We have to convert this to,
‫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 double question mark in our 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.
‫Okay?
‫I'm going to do the same thing for secondNumber as well.
‫So I'm going to say, "Let's secondNumber
‫is going to be the integer
‫of secondText.text", okay?
‫Int secondText.text.
‫So that's okay.
‫And, we are going to sum this 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, right?
‫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 integer optionals,
‫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 are going to get a string from there, right,
‫an empty string, we are 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 us a nil value, it's going to,
‫at the worse, it's going to return us an empty string.
‫And we are 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 are 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 firstNumber,
‫it will make this integer an optional integer, okay?
‫And this is okay for Xcode, this is optional.
‫But if I put this, this is going to be, and not optional,
‫this is going to be a regular integer.
‫And by this time we are betting, we are 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 resultLabel
‫and I can say, "resultLabel.text
‫is now result."
‫Okay?
‫But I cannot say that because resultLabel.text
‫is expecting a string from me
‫and I'm giving an integer to it, right?
‫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, right?
‫This is going to be okay anyway.
‫So let me run this and let's see if this works.
‫So we are gonna get the firstText.
‫So let me give this something like 40 or, five.
‫So let me give this 10, let me just sum them together,
‫and here we go, the 15.
‫So let me give minus 10 and minus five.
‫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", okay,
‫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 in integer is a nil,
‫the application crashed.
‫And the good thing is,
‫you know how to make this safe, right?
‫You know what to do in a a situation like this.
‫Of course you can go with double question marks
‫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 are gonna do that in the next lecture.