The course is part of this learning path
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
Prerequisites
To get the most out of this course, you should have some basic knowledge of iOS.
Hi, within this lecture we're going to continue working on playgrounds, and we're going to learn about variables. So far, we have been creating variables as an example like this, so like var str is a variable, myNumber is a variable. So, let me take a note here. If you do two slashes, you can take a note like this, //Variables & Constants. We're going to learn what a variable is right now. But this is important as well. If you add two slashes before a line, it means this is a comment so you cannot type whatever you want in a coding section. So, this is all relevant codes, but if I do something like this, so if I write something random like this, if I click the 'play' button over here, it won't play because it won't recognize what I'm writing. So, if you take a note, just add two slashes. So, we call this a comment string, a comment line in our code. So, what is a variable?
As you can see we can create variables with keyword var. But why do we need them? Because we cannot give everything in the code. Maybe we want to take some input from the user, or maybe we want to get some data from the internet, we want to download something so we cannot know what's going to happen. We cannot anticipate everything. So, we need to store those values in order to work on them later on. For example, if you're going to build an app like this, you're going to take the input of the user for their name. So, the user will just enter their name into a text box, and then later on they are going to enter their surname, and we're going to just display this name and surname together. So, in order to do that, you cannot just come here and you cannot just say name is this, name is that, or just show this, just show that. You have to create something in order to hold this value inside of it. So, this is a variable. This is what we call a variable. For example, we are just multiplying this 10 * 5 here, and it shows us the value of 50 but maybe we want to multiply the age of the user by five. So, when I'm writing this code I don't know the age of the users. So, I need to create something like, var myAge then multiply it by five. And let me do this by example so you will understand it much better.
So, when I create a variable, I use var keyword. For example, if I say var userName, and beware that I'm using the second word with capitalized letters. So, if I say userName, the N will be capitalized. Why are we doing this? I'm going to show you, don't worry. For example, right now, I'm going to assign my userName to be "James". So, why I'm using a capitalized letter. So, this is a convention. This is what we generally do in swift programming language. Every programming language has their own convention. So, let me take a note here. So, we have something called snake case, if we follow this convention, we will do this in a different way, like that user_name and this is a snake case convention. This is a snake case name. So, if we do something like this, var user_sur_name, with underscores, it will still work. It actually doesn't matter what we name our variables. But generally, we don't do that in Swift. So, when you go to the internet, when you search for things, you won't see that, you will see this. So, I'm going to follow the convention of Swift, which is a camel case.
So, this is called snake case, and this is called camel case. And again this is not mandatory, but we are doing this in order to make our variables more readable by coders, by developers. So, I'm going to follow this norm in here throughout the course. So, that's why I'm using capitalized letters. So, for example, I created a user name called James and I created as user surname in the name of Hetfield. So, anytime I can change those variables because they are variables. I can come here and say, user name is not James anymore, user name is something else. So, I can do whatever I want to do with James because this is a variable, right? Because I stored this value into user name, and I stored this Hetfield value in the user name string. And the string means basically a text, a text value.
So, what if I want to change this user name? If you type userName, and if you say this is not James right now, this is "Lars". If you hit 'shift + enter' rather than hitting the 'play' button, it will automatically run these codes for you. You can do 'shift + enter' when you type something, like you can say print user name and you can say print(userName) here as well, and not users show name user name as you can see if I type user I can hit 'tab' or I can just double click on whatever I want to choose. And now if I do 'play' or 'shift + enter' it will play it for me. For example, as you can see for the first time this run user name print user name and it just gave me James. And for the second time it gave me Lars because I changed the user name in here. And this tells us something, these codes are actually run with order, right? They're running with chronology. So, it is running, the first command then the second command, then the third command and the fourth command and the fifth command. And because of that, first I have seen the James as a log, as a print output, and then I changed this James, then I saw Lars.
So, for example, if I go to the top and if I want to change the user name, I cannot do that because the user name won't be even defined in here. So, if you could 'play' button over here it will give you an error. It will say this is an unresolved identifier, because why? Because I have initialized, I have defined this in the 10th row, in the 10th line. User name isn't even defined in the eighth line. So, once you define this with var keyword, then you can change it, you can play it, you can do whatever you want to do with it. And this is for variables, we are we also have constants, and we use let keyword for that. So, it means that we're going to define this value, and we're not going to change it afterwards. So, for example, let's say that let userAge = 50 now I can reach the user age when I type user age, but I cannot do this, I cannot say user age is now 30 because I have defined this as a constant. As you can see, this is unresolved because I have misspelled it as you can see I have to run A with capitalized A.
As you can see it says that you cannot assign this value because 'userAge' is a 'let' constant. So, this is not a variable anymore. This is a constant. So, we have to create the variables with the var keyword if we want to change them afterwards, and we want to create variables or constants with let keywords if we don't want to change them afterwards. So, why do we need constants? For example, if we are going to use something like pi number in our app, we won't change it forever. Right? So, pi is constant like 3.14 and it doesn't make any sense to define this variable with the var keyword. So, we need a 'let' here. So, we multiply 10 * 5 before, can we multiply userAge * pi. In fact, we cannot do that because they are not even the same type.
So, here comes a new concept. So, there is something called 'type'. And as you can see one is 'int' integer, and another one is 'Double'. So, we're going to learn what those mean in the next lecture, don't worry. But for right now know that variables are not the same. So, we have texts, strings like James or Hetfield or Lars, and we have Doubles or Integers. So, if you put that over here in the userAge value then it will compile and it will just do the multiplication for you and show the results. As you can see, now they are the same type. So, now maybe you understood that numbers with decimals and numbers without decimals are not the same type, and that's correct. So, we're going to stop here, and within the next lecture, we're going to discuss what are some different kinds of labels that we can operate with in Swift.
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.