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!
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 see how we can predefine the variable types, okay? So far, we have been trying to create variables and Swift was trying to understand what kind of a variable was that, right? Because we have said that let my number is four. Now, we're going for part two, okay? So, this was part one and now I'm going to go for part two. I'm not trying to create another playground over here because we basically try to continue the same subject. After that, I'm going to create another playground for another topics. For example, if I want to create a string like var myString, if I do something like this with a column, I can say this is going to be a String with a capitalized S.
So, maybe right now you're thinking that you have talked about camel case and the snake case and they all start with lowercase letters. Why are we going for uppercase letter in this case? So, this is a class name. And right now, we don't even know what a class is but classes are very important aspects of object oriented programming languages like Swift, like Java, like Python, and we can call Swift an object oriented programming language. So, this is a class name and we are referring to this string class and we are starting with a capitalized S rather than a lowercase s, okay? So, classes are the only way that we go for capitalized letters in the Swift. So, you have to write this like this String and you can just choose the String and then later on you can do something like this. You can put the equal sign over here and you can just give the value of this variable over here. Can I do something like this? Like 50? No, I cannot because it will give me an area and it will say that this is a string. You cannot just assign an integer type over here, right? Because this is going to be a string. Can I do that? Of course I can. This is not a number.
If I put quotation marks around this, it means that this is just a text. It's not an integer, it's not a double, it's not a number, it's just a text right now and Swift will not recognize this as a number. If I try to multiply this by any number, this won't even work for me. It will say that you cannot apply this with different types. You cannot multiply a string by an integer. So, this is not going to work, right? So, this is a string and this won't work obviously. So, you can just say something like quotation marks, inside of it, you can write whatever you want and it doesn't matter if I do this var or let. As long as I pre-define the value over here, it won't make any difference. So, let me do a number here. So, I'm going to call this another number and this will be an integer this time. And as you can see, when you type Int, you will see in 8, 16, 32, and 64. And these values are referring to the bits. So, higher the bit, 64 bit for instance, the higher the capacity to store the value of a number.
So, you cannot assign something like a billion or a trillion in the integer eight but you can store this in integer 64. Don't get confused about this. We're just going to go for Int right Now. But, if you see something like Int 8, Int 16, it refers to the bit, capacity to store of the number. So, I can say that, right? Int 10, but can I say 10.5? No, I cannot say that because 10.5 is not even an integer. It's a double, okay? And as you can see, it gives us an suggestion you can use. It says that you have to wrap this around an Int. So if I do it, what will I get? I will get 10. So, when you cover, right? And
you wrap this around something, it will try to assign this value to be an integer, okay? And let me show you another example of this. Let me create another variable called stringNumber for example, okay? And I will try to convert another number or 10 into a string. Can I do something like this? Can I wrap this number with string class? Can I do something like string, open parentheses, and say 20 for example. I can do that right? And it will convert this 20 to be a text. So, this is not different from putting quotation marks around this 20. As you can see, it puts it for us. So, this is a text. This is not a number anymore. So, I cannot just go over here to stringNumber and multiply it by another number, because string number is now a string rather than an integer.
So, I can convert this to be a string if I want and as you can see, I can do this and it won't give me any error because I'm getting, I'm assigning this value to be a string, not an integer. So, can I go and say that wrap this myString with integer? I can do that, right? Because I can convert the number 50 to be an integer. It makes sense. But, what if I was assigning this value to be James rather than 50? Can I convert James to be an integer like this? No, I cannot do that because it doesn't make sense to convert James into an integer and this will crash my app actually. And we're going to see a lot of these examples in the optionals and in the upcoming apps that we're going to build, so I'm not going to go in depth for that right now. I'm not trying to convert strings into integers. So, I'm not going to do anything that may fail at this time.
But for example, let me show you what defining and assigning means. So, defining is different than assigning. In the 41st line, I create myString and I assign a value, okay? But, maybe for some reason I want to, maybe I can, I may want to create a string but not assign a value at that moment. Maybe I'm going to download this value from the Internet, okay?
So, I can do something like this. Let myVariable is going to be a String type, but I'm not going to assign anything to it right now. So, this is perfectly okay. I can just leave it like that, okay? If you play this, there won't be any problems. But the problem starts if you try to do something with this because we haven't initialized it yet. So, initialization is different than defining the variable. So, this is defining, okay? I created myVariable but it doesn't have anything right now. It doesn't have any value. It isn't even initialized yet. So, if I want to initialize this, I can come over here and I can change the value or I can assign any value to myVariable but I cannot do something like this. I cannot just say go and make this uppercase. It will give me an error, okay?
I did that in here because it had a value but right now, it doesn't have any value. And as you can see, it says that constant myVariable used before initialized. So, I didn't initialize this but I tried to make it uppercased. So, it doesn't make sense as well, right? So, it gives me an error. So, you have to be careful when you initialize the variable, when you define the variable, and when you access the methods or attributes or options like this. You have to do this after you initialize your variable. And in fact, we're going to have a lot of examples like this when we try to build an app. So, if I do something like this, myVariable is now Test, now, after that I can do whatever I want to do with myVariable.
I can just go for uppercase, I can just go for lowercase, whatever I want to do. And as you run this, as you will see that Test is now TEST and even if I did create this with let, I'm not changing the value over here. I'm just initializing it. I'm just assigning a value for the first time. Now later on, I cannot do this, I cannot change this value again because this is created with let. This is a constant. As you can see, it says that it's immutable, okay? Immutable means it cannot be changed, mutable means it can be changed. So, I believe now you have understood how you can pre-define the values, value types actually, and how you can define something and then initialize it later on and how you must initialize it, then use some methods and use some variables or any other options that you might want to do. So, if you do print myVariable over here, you will only see Test, okay? Because this myVariable.uppercased doesn't change the original myVariable, okay?
It just produces another constant, another variable maybe. So, in fact, I can just create another variable and I can have it assigned to this value like that. So, I can just say let my upper case or upper variable is now this, okay? So, myVariable.uppercased will be stored in my upper variable let. So, if I just print my upper variable over here, I will see the uppercase Test. But if I print myVariable, I will see the lowercase test. So, I'm not changing the original value of myVariable. I'm just creating another variable over here and if I want to, I can just re-assign this to another variable or another constant. So right now, I cannot just do this again because I have initialized this. I have created this as a letter, as a constant. So, now you know the difference between constants and variables.
You know the difference between defining and initializing. You know how you can assign object types when you create variables or constants. Actually, you have learnt a lot during these two lectures. It is time to stop, in fact, because within the next lecture we're going to learn about data structure called arrays in which we can store some values.
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.