Safety and Scope


The course is part of this learning path

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 make our calculators safe. And later on, we're going to focus on a subject called Scope which is very important in Swift. So, we're going to 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 first unwrap this, this will be a regular Int so I can just sum them together but it crashes my application when 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 flat. 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. 

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. 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 going to check for the second one and if that works, I'm going to do what I'm going to do. So, this will check to convert firstText.text into an integer. And if that succeeds, it's going to do the second one and then I can say "firstNumber + secondNumber. And as you can see, firstNumber and secondNumber is not optional anymore. So, I can go for resultLabel.text = String(result). 

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 3 to 2. They will see 5. So, this works. So, we didn't break anything. So, let's try Apple and hit + and as you can see, this doesn't give me a crash. And of course, you can have an statement over here and you can say, resultLabel.text. Bring me a number. Give me a number. Don't give me any string. 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're 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 pass it over here and say - rather than +. I'm going to do the same thing here with * and then over here with divide sign. 

So, here we 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 first calculator ever. 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're going to be talking about Scope. So, what does it mean? So, let me tidy up here a little bit 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 users sees the app. Whatever I write here gets executed.

So, we don't need that in this application so it stays empty. We don't need anything to happen when the first app launches. We need something to happen when user clicks on some buttons. 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. One to two, 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 = 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 myNumbers 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't just call them same. I can't 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 minus collect. And is there a way to create a variable and 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 the other classes. 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 first text, second text and result label under my class but they are 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't 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 are 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 results - "firstNumber + 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 results in my class, in my sumClicked function, everywhere. So, we're 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 lat, 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 identify your 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're 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 2 by 3, it gives me 0 because this is an integer. But if I just divide or sum this Apple with 3, it doesn't crash. So, that's important right now. And we have completed our first step so, congratulations. And following the next section, we're going to work with layouts a little bit more and then we're going to start working on real apps, real examples.


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