‫Instructor: Hi.
‫Within this lecture, we're gonna make our calculators safe,
‫and later on,
‫we are going to focus on a subject called scope,
‫which is very important in Swift,
‫so, we are gonna see what it is in just a few minutes.
‫So first of all,
‫let me start by just solving this problem that we have,
‫if I force unwrap this,
‫this will be a regular int,
‫so, I can just sum them together,
‫but it crashes my application,
‫then, user gives me a bad input.
‫So, how do I go for that?
‫If I just delete this force-unwrap thing,
‫then, it will give me an error.
‫So, of course, I'm going to go with this if let,
‫okay, so, we have seen this, what does it mean?
‫If you can convert this firstText.text into an integer,
‫then, cast this to a variable called firstNumber,
‫assign this to the variable called firstNumber,
‫so, I can work with this firstNumber.
‫Then, I'm going to do that for secondNumber as well
‫inside of this if,
‫I'm going to delete the second optional force unwrapping,
‫sorry, over here,
‫and as you can see I have an if control,
‫and inside of this,
‫I have another if control, so that's okay,
‫I can have as many as I want.
‫So if statements can be layered as this,
‫so first if statement,
‫if this works, then, I'm gonna check for the second one,
‫and if that works I'm gonna do what I am gonna do.
‫So, this will check
‫to convert firstText.text into an integer,
‫and if that succeeds, it's gonna do the second one,
‫and then, I can say firstNumber plus secondNumber,
‫and as you can see,
‫firstNumber and secondNumber is not optional anymore.
‫So, I can go for resultLabel.text is String representation
‫of the result itself,
‫so, let's try this way,
‫and let's see if that works for us.
‫So, the user will come over here,
‫let's see, first three to two,
‫they will see five,
‫so, this works,
‫so, we do not break anything.
‫So, let's try apple and hit plus,
‫and as you can see, this doesn't give me a crash.
‫And of course, you can have an else statement over here,
‫and you can say resultLabel.text give me a number,
‫don't give me any text; don't write apple here.
‫So, you can display a message to user in a way like that,
‫but this is perfect for right now,
‫this is what I have been looking for, so, this works,
‫and the rest is pretty much the same:
‫we are going to actually copy all of this
‫and paste them into the other functions as well.
‫So, I'm going to copy all of this with Command + C,
‫and with Command + V I'm going to paste it over here,
‫and say minus rather than plus,
‫I'm going to do the same thing here with multiply,
‫and then, over here with divide sign.
‫So, here you go,
‫we actually made our first calculator,
‫so, let's go and test this.
‫Of course, I have casted everything as integers,
‫maybe we can do it with doubles or something,
‫but right now, this works in a way that we want.
‫We don't have to make this complicated
‫because we are building the worse calculator ever, right?
‫So, that's okay.
‫But since we are here,
‫let me tell you one of the most important concepts in Swift
‫in this lecture:
‫so, we are going to be talking about scope.
‫So, what does it mean?
‫So, let me tidy up here a little bit,
‫okay, so, we can see it better.
‫I'm going to write some codes over here as well
‫because I'm going to work on scope,
‫and by scope, I mean, the scope of the variables.
‫So, here, I'm defining some firstNumber, secondNumber,
‫and everything in sumClicked function
‫and doing the same in minusClicked,
‫and I can have as many functions as I want in here:
‫I have this viewDidLoad;
‫I have this sumClicked;
‫I have this minusClicked;
‫and I have these variables over here.
‫So,
‫what if I want to create a variable inside of one function?
‫And can I reach that variable inside of another function?
‫So, for example, in viewDidLoad,
‫what was viewDidLoad, again?
‫So, this is the first thing happens when the app is loaded,
‫when user sees the app,
‫whatever I write here gets executed, right?
‫So, we don't need that in this application,
‫so, it stays empty.
‫But we don't need anything to happen
‫when the first app launches,
‫we need something to happen when user clicks on some button,
‫so we have written our codes inside of button actions,
‫but for example,
‫let me create a variable inside of this viewDidLoad.
‫So, I have a number over here,
‫I can change its value, 1 through 2, for example,
‫and this is perfectly okay,
‫I have created this,
‫I have changed its value,
‫I can do whatever I want to do with this,
‫I can make my resultLabel.text
‫equal to string myNumber,
‫for example,
‫or I can just print this,
‫and that's perfectly okay, this will work.
‫But what happens if I want to change the myNumber's value
‫inside of my sumClicked function?
‫Can I do that?
‫I cannot do that, actually.
‫And this will give me an error soon,
‫as you can see,
‫it says that this is unresolved,
‫and why is it unresolved?
‫Because it's not in the scope of sumClicked function,
‫it's in the scope of viewDidLoad.
‫So, we have created this variable inside of one function,
‫we cannot call that variable in another function,
‫so, I cannot do that.
‫And this firstNumber in here is completely different
‫than this firstNumber in here,
‫so, these are two different variables over here,
‫I can just call them same,
‫I can just call them different names,
‫it doesn't matter
‫because they're completely different variables anyway.
‫So if I create a variable in the sumClicked,
‫I cannot change that in the minusClicked.
‫And is there a way to create a variable?
‫And I reach this variable in all functions,
‫why I cannot do that?
‫Of course, there is a way to do that.
‫And in fact, we are doing that right now.
‫We have this class,
‫and we haven't seen what the class is,
‫we will see in a separate section; in a detailed section.
‫But as you can see,
‫this class has some different functions,
‫different variables inside of it,
‫so, this should be something important,
‫and we are doing everything inside of our class.
‫So, I have defined this firstText, secondText,
‫and resultLabel under my class,
‫but they're not even inside of a function right now,
‫so if I come under my sumClicked, I can call them;
‫if I come under my minusClicked, I can call them.
‫So, these variables are actually in the class level,
‫these are not in functions,
‫and in fact,
‫we couldn't drag and drop these views
‫into the functions as you might remember,
‫we couldn't drop them inside of viewDidLoad,
‫we have to make sure that they're defined
‫inside of the class.
‫So if I do the same thing,
‫I can create this number, for example, in here,
‫and I can reach that number
‫inside of every function that I have,
‫so, I can create a result in here with zero initial value,
‫then, I can call this result anywhere I want.
‫I can call this under viewDidLoad,
‫so as you can see now,
‫I can change this result value,
‫so, I have defined and initialized this under my class,
‫then, I can call this value,
‫and I can change its value from wherever I want,
‫so, rather than creating a new result over here
‫I can just assign my result firstNumber plus secondNumber,
‫so, this is going to be this,
‫as you can see, if I click on it, it will be highlighted,
‫so, this refers, again, to this variable over here,
‫so, we are using same result,
‫in my class, in my sumClicked function, everywhere,
‫so, we are using the same variable.
‫We can do that under other functions as well
‫so that I can reach and use the same variable
‫in my old functions.
‫It is much more efficient to do this in this way
‫rather than creating new result variables.
‫And it works like this,
‫when I type result,
‫it first looks,
‫if I created a result inside of this function,
‫if there is a result variable
‫that I have previously created here,
‫then, it works with that variable,
‫if it cannot find a result, it looks over here.
‫So, if there is another variable in class,
‫and if it cannot find,
‫it actually looks outside of the class as well.
‫So, I can create a variable outside of my class as well,
‫and this is called global variable,
‫we're going to work with these global variables later on:
‫but we, generally, do not prefer to do this very much
‫in our applications because it's not very safe,
‫we can easily make a mistake over here;
‫but this is valid as well
‫if it cannot find the result inside of the function,
‫inside of the class,
‫it can look to find the result outside of the class as well.
‫And if it cannot find outside of the class
‫it actually looks for predefined values like let var,
‫so, is there a keyword?
‫Or is there a predefined Swift value in here?
‫And if it doesn't find anything
‫it gives me an unresolved identifier error,
‫it works like this.
‫So, make sure you create your variables
‫inside of your function,
‫or inside of your class,
‫or outside of your class
‫whenever or wherever you may want to create,
‫so, decide the optimal place in your own case.
‫We are going to see a lot of examples regarding to this,
‫I'm just explaining how it works right now,
‫don't worry about that,
‫so, as you can see, this works,
‫and if I divide two by three it gives me zero
‫because this is an integer,
‫but if I just divide or sum this apple with three
‫it doesn't crash,
‫so, that's important right now.
‫And we have completed our first app,
‫so, congratulations.
‫And following the next section:
‫we are going to work with layouts a little bit more;
‫and then,
‫we are going to start working on real apps, real examples.