Check out a free preview of the full iOS App Development with Swift course

The "Variables" Lesson is part of the full, iOS App Development with Swift course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano discusses defining variables in Swift, including constants, types, explicitly defining data types, and core data types. Swift will infer and enforce variable types but can be overridden with the Any keyword.

Preview
Close

Transcript from the "Variables" Lesson

[00:00:00]
>> So as we mentioned before, Swift is anesthetically type language, okay? So variables as we saw before, they're using the keyword var. Var, the name, and that's all. Constants are using the keyword let. And if you're a JavaScript developer, be very careful. Because you know that in JavaScript at least from ES6, or w ECMAScript 2015, let, it's actually defining the variable in JavaScript.

[00:00:31]
But here let is a constant that can actually be used for a constant and also for an immutable variable. I don't know if you know the difference, but actually a constant needs to be defined at compiled time, and an immutable variable is something that you can define in execution time.

[00:00:48]
You can go to a database, retrieve a value, and store that value in an immutable variable. After it's set you cannot change it. Well, let can be used for both, okay? And most of the time we're going to use let. I can say that probably nine of ten times we use let, not var, okay?

[00:01:08]
Because typically we don't change values of most of our variables. So we make them an immutable, okay? So it's just let instead of var, just that, okay? Time types are defined with colon after the name. So yeah, this is new. So if I have a name, I say colon, a string, typically we use a space after the colon.

[00:01:34]
Of course you can do however you want this because spaces are not important in Swift. Means so like Python where space is so important. Now, here you can have more or less spaces, but typically we use this syntax. If you have experience with TypeScript, it's kind of similar.

[00:01:51]
The same as ActionScript. Remember that from the Flash error? It's actually using that syntax. Okay, so let name string means that I'm going to have a constant of type string. But as we saw before, most of the time we don't need just press a type, okay? Because that's how Swift infers the type.

[00:02:15]
If I'm setting a value like this, if I'm setting the value like this, okay, it's inferring the type. How do you know the type that is being inferred here? Option click, Option click over the name give me the type. Here it says string, okay? That's how it's inferring that.

[00:02:45]
But it's actually inferring the type. It's not like JavaScript where actually then we can store something different. It's not gonna work here. If it's a string, it's a string. Actually there is a trick as on a lot of languages. There is a type, and because it's a type, it's title case.

[00:03:04]
So we start with uppercase, quote, Any. Any means like a JavaScript variable. Any also exists in other languages, such as even TypeScript, okay? So Any means, well, actually, well, here it's give me an error because it's actually a let. So it's not a data type error, it's saying to me, hey, it's a let, so you cannot reassign the value.

[00:03:28]
But if we change to var I can actually change that. The problem with Any is that then the language doesn't know what it is. So if I wanna use that, I can even create an array now, an empty array, okay? So that's how you create an empty array.

[00:03:49]
So the problem is that when I am trying to use it, I don't have properties, I don't have methods. I need to cast. And we are getting into a dark place, okay, because we don't wanna do this unless we have to, okay? And there are some special situation where we have to define any, but we don't wanna do this.

[00:04:12]
Okay, so we don't wanna use any as a data type. So as I mentioned before, types are defined with colon after the name. So something like this. But sometimes we have a problem. Let's say that prices are always double. By the way double, it's a double precision float, okay?

[00:04:31]
So a decimal with more precision. So let's say that I have fair prices, and I want all of them to be double. So price, it's actually double, okay? It says double. Other price, it's also a double. But a fair that I have here, I'm using, remember, Option click, says int, integer.

[00:04:55]
Do you know why it says integer? Does anyone know why it says integer?
>> The other two were maybe floats and it's considering an int as just a different type of value [CROSSTALK].
>> Yeah, the other two, well, the first one didn't have a value, but we set explicitly the type.

[00:05:15]
The second one has a .99. And yeah, that's a double by default, it's a literal double. But now 23, by default it's a literal string. I'm sorry, integer. So 23 is a literal integer.
>> What will happen if you went next line and changed the value to some float?

[00:05:38]
>> Okay, so you wanna do now 23.99?
>> Yeah.
>> It's not gonna work. I mean, if I save this, it's going to compile. And it's saying, you cannot assign a value of type double to a type int. Because the variable was defined as integer, you cannot change the type later.

[00:05:56]
So, then we need to change this. There are two ways to solve the problem, .0. Yeah, that works. That becomes a double now. Or we can explicitly state the type. And they will say, okay, I know that mean the literal now is an integer, but that integer can also be used as a double.

[00:06:17]
So it works, yeah?
>> Can you show again how you find the inferred type?
>> Yeah, use the Option key. When you use the Option key, let me zoom in a little bit. So you can see that the cursor becomes a question mark with the Option key. You keep the Option key pressed.

[00:06:37]
Then you hover and then you click with the Option key pressed. And then you'll see a dialog with the current information. You can see it says no overview available. If you're using the recent special documentation comment system similar to Java doc and many others that you can set, so then you can actually see summary information or help information for the developer from that variable.

[00:07:05]
Okay, makes sense? Cool, well, they are called data types. We have a string, capital S, be careful with that. All the data types in Swift starts with capital letters. Integers, we have also bit specific numbers like int 32, int 8, like a byte. And also we have unsigned integers.

[00:07:29]
They are only positive numbers. We have double. We also have a single precision float, it's called float. We don't typically use it a lot because the framework and everything inside will actually use double most of the time. And finally we have Bool. Not Boolean, it's called Bool, okay, that's the type.

[00:07:50]
And we use the value of true and false and values are in lowercase. For a strings, if you're creating a strings we use double quotes. We cannot use single quotes. So if you're a single quote person, I'm sorry. So it's double quotes for strings. And something interesting is that within any string, you can have a template expressions similar to other languages.

[00:08:14]
I know if you're using JavaScript you have dollar sign and like CodeBlock. Well, here is backslash parentheses. So within the string, backlash parentheses will actually accept any expression, okay? So it can be a variable. And if it's a variable, like for example a price, if it's a double it will convert that value into a string.

[00:08:40]
Either need to make the conversion manually, okay? So it's gonna work automatically. To finish the basic data types idea that we're going to use later in our app, we have tuples. So there is actually an interesting data type. A tuple is defined with parentheses on a comma separated list of other types.

[00:09:05]
So, for example, I create a variable named coordinate as a tuple of join integers, okay? And how do you access each value, with .0 and .1, okay? I know that sounds weird. So this is not an array, so we don't use an angle, something like this, okay? We don't wanna do this because it's not an array.

[00:09:33]
It's just .0 and .1. But be sure that you are looking at the next sample that we have here. Of course you don't need to use the same type. Any time will work, so I'd say that the city is a string or an integer, like the city name and a population.

[00:09:51]
And look at that underscore there. You can use the underscore in integers and double literals for my separator. This is just for human visibility, okay, to improve how we humans read numbers. That's all, okay? So it's actually the same if you don't have that underscore. It doesn't make any difference, okay?

[00:10:15]
When you're creating tuples, something that you can do is also to pick a name for each section in each segment of the tuple. So here I have in a state with a name and population, and is nice, okay? So it's a couple of three segments. This is not a class.

[00:10:35]
This is not a structure. It's just having one variable name with three variables inside, okay? But it doesn't contain any other thing like when we have classes, okay? It's just a simple way to create, I mean, in JavaScript you create an object typically to do this, okay? You use JSON to create an object quickly.

[00:10:57]
But here we have tuples, sir?
>> It's a class without the methods, it's just like the data part of it, kind of?
>> Kind of, why this is not actually like a class without methods? Because what happens if I have a function that receives in a state, like print the state, okay?

[00:11:15]
Well, we haven't seen functions here yet, but let's fast forward a little bit. We'll receive state, okay, and type, what's the type? Well, actually the type is this. So this is the type. Okay, so I need to copy this. There are better ways to do this. But anyway, that's the type, including the parentheses.

[00:11:34]
Because the parentheses means tuple, okay? So that's the type. The thing is that when you're sending the state as an argument, so I call print the state, this code is not gonna work for now, okay? So just believe me, I will explain why it's not gonna work in a minute.

[00:11:52]
So I sent that state. So it's actually making a copy. So it's passing the tuple, not by reference, but by value. So an object from a class, it's passed by reference. These are just values. So which means that if you can somehow change the name or the population within the function, you're not changing the original variable because it's a copy, okay?

[00:12:30]
We will see these in action later. And why this is giving me an error? I can give you a hint, okay, of why this is giving me an error. In fact, it says, missing argument label. And you're not going to like this at first sight. So you're going to see these for the first time, you will see, what's going on?

[00:12:51]
It's asking me to insert a state colon in the function call. State, it's actually the name of my argument. I have some simple examples just to actually explain this, okay? But just because I did this just to finish, I mean, the answer, it's not the same as a class because these are just values.

[00:13:14]
And they are being copied every time you are passing this or returning this from one function to another.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now