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 see different kinds of variables. So far, we have been talking about some types, right? We have talked about Strings, for example. So, let me take a note over here so that you can follow it better when you finish your app, finish your section. Okay. So, this is a String section and this is a String. When you put a quotation mark between values, it makes them a String, it makes them a text. Okay, so this is not anything fancy, this is just a text, we call text 'Strings'. So, let me delete all of this and I'm going to make this into a String section. So if I say, userName., it means that I'm going to reach the methods and the attributes, the properties of the String, user name. So, what do I mean by methods or properties or attributes? We didn't learn any of these terms. It basically means options that we can do with the user name.
Okay, so user name is a String and when you hit that you can get some options regarding to String objects. So right now, I can do whatever I want to do with my userName. For example, in here you will see something called 'append'. If you click one of this, append, and if you add a character between quotation marks, of course, like o, okay, if you hit the 'play' button or 'shift enter' then you will see that this will add something. This will add the 'o' letter to James. And if you look over here, you will see that James is now Jameso because we have appended an 'o' to James, so that's another feature of variables actually. You can reach their methods, you can reach their options or attributes and you can do whatever you want to do with that.
For example, you can see a lowercased option in here, we have J as capitalized in our String, right? If I choose userName.lowercased, it will make J lower case as well. So here you go, we have a small Jameso, I can do the same thing with uppercase of course. So go down below, you will see the uppercase if you double click on this, you will see on the right hand side after of course hitting the 'play' button, you will see JAMESO with uppercase letters. So, that's the video of variables working with variables and that's the beauty of working with Strings. So, we took one String like James and we appended something, we made them lowercase, uppercase then everything.
And of course we can do much more with the Strings and I'm not going to go into every method, every attribute, every aspect of the Strings in this lecture because we're going to learn a lot while we are on the route to the end. We're going to work with Strings and other variables in all projects that we're going to have anyway. Later on, I'm going to go for numbers. Okay, so integers, doubles, and floats and doubles and floats are actually numbers with decimals while integers are whole numbers and they are completely different from each other as long as the technicality of the Swift is considered. So, if I do something like this, it will think that user age is an integer.
Okay, this is an integer, this is a whole number, it doesn't have any decimals and this is a double because this has some decimals and as you can see we cannot interact with each other. We cannot multiply an integer with a double, of course there are ways to overcome that problem and we will see them later on, don't worry. For example, let me create a number like four. Okay, and this will be again myNumber. Now I can see, I can just say that userAge divided by myNumber. And what will it give me? It gives me 12 and 12 multiplied by four. Actually it goes to 48, not 50. Right? But since these are integers, again the result will be an integer as well. So, I cannot see the decimal and this might give me something wrong like this. So, this is not very correct, right? This has to be 12.5, not 12. But since I'm doing this with integers, I'm getting this result as expected. So, what can I do? How can I just calculate this in a right way, right?
So, let me do it with doubles for example, let me say that userAgeD for double. Okay, and I'm going to set this to 50.0 pand I'm going to set this to 4.0 now there are doubles altogether. And if I just do something like this userAgeD divided by myNumberD. So, let's see what will be our number results. So, let me click on the 'play' here and on the right hand side, we're going to see 12.5. So, this is what I wanted, right? So if I want exact results like this, then I'm going to go for doubles with decimals instead of whole numbers and there is actually a method to convert an integer into a double or a double into in teacher. And actually there is a method to convert everything to everything. As long as it makes sense and we're going to see that later on. Now, let me tell you about something called boolean. So, this is one of the most basic things in programming. So, this is either true or false. For example, if I create my boolean variable, I can set it to false, as you can see this says that this is a bool, boolean. Okay, and this only has the value of false, or true. So I cannot do, I cannot just go and say my boolean is five anymore.
Okay, because this is a boolean but I can say my boolean is true and why do I need this? This is too basic. Maybe I'm doing a game, maybe I'm just controlling something like if my character is alive or dead so I can just do my character is alive and I can just go and say my character is that and I can do something like if controls, we're going to learn what they are, something like if my character is still alive do this or if my character is that do that. So, these are actually a basis for logical expressions. Okay, and it's going to be much more clear to you once we use those variables in real examples, but for right now just try to understand what they do stand-alone values. We're going to see how to use them in real apps. Okay, so far we have been learning a lot and we have learned about integers, doubles and maybe you realize that when I say let's use your age is 50, the Swift is actually understanding that this is an integer, because I have initialized this to be an integer, right?
So, what if I want to change this type, if I say something like my number is not for anymore, but it's rather James, will it work? Actually, it's not going to work. So, let me change this to var for example. Okay, so this is a variable rather than a constant and let me run this one more time. So, it will be stated as var, so I can come over here, I can just say var my number is 3.0, right? And this will work. But what if I say myNumber is Kirk rather than 3.0. And as you can see, it says that you cannot assign this value of type 'String' because this has the type of 'Double'. So, it means that once you assign a type to variable, it just takes. So, in some of the programming languages, this is allowed like in Python. But in Swift, this is not allowed. Once you define the variable, you shouldn't try to change the type of the variable anymore. So, let me say this and stop here. Within the next lecture, I'm going to try and deep dive into this type thing a little bit more.
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.