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 learn about dictionaries. So, we have learned about arrays and in arrays, we have ordered collections, right? We have indexes. So, we have learned about sets and in sets, we didn't have ordered collections. We had unordered collections but we had unique elements. And inside of dictionaries, we will have key-value pairings. So, we're going to have a key and we're going to have an assigned value of that key inside of a dictionary. So, maybe you cannot imagine what it looks like right now. But you will understand why and when we are going to use dictionaries once we do the example. So, suppose that you have a favorite movie array one more time. But rather than just saving the favorite movies, you want to save the respective directors of that movies as well.
For example, rather than just saying Pulp Fiction, you have to say Pulp Fiction, and Pulp Fiction is directed by Quentin Tarantino. So, how do you do that? You can put those values into two separate arrays. You can have one var myFavoriteDirectors array over here, in which you will list your favorite directors like Quentin Tarantino, Guy Ritchie. And then you may create another array in which you put the respective values for those directors. Like you can go like this Tarantino and then Guy Ritchie and I can create another array just like this. And in the first element, I can say Pulp Fiction and in the second element, I can say Lock, Stock and Two Smoking Barrels. And this would work but it wouldn't be as efficient as a dictionary. Because I can go like this in a dictionary, Pulp Fiction, and Tarantino.
So, all you have to do is just put a column between key and the value. So, the first one is key and the second one is the value of that key. For Guy Ritchie, I'm going to go Lock, Stock and hit a column and I will say Guy Ritchie for example. So ,let us do one more example over here, The Dark Knight. And this will be directed by Christopher Nolan. Christopher Nolan. So, this is a dictionary. It's very similar to array but you put a column between key and values. So, if you do myFavoriteDirectors as you can see this is now a string to string dictionary. And key is string and value is string as well. Of course, it could be string to integer, integer to string. It doesn't matter. So, can I do this? Of course, I can't because dictionaries do not have indexes. You could have gone for myFavoriteDirectors, Pulp fiction and that would be okay.
So rather than giving indexes, I give keys in here. So, when I provide the key it will return the value to me. So, I can go for The Dark Knight for example, it will just say Christopher Nolan as you might see on the right-hand side. So, this is how you work with a dictionary. This is how you create, this is how you operate with dictionaries. For example, I can go and say that Pulp Fiction is not Tarantino anymore. It's Quentin Tarantino. And this will change the value of the Tarantino. This is now Quentin Tarantino. If I go over and just print myFavoriteDirectors dictionary, I will see that the Pulp Fiction is directed by Quentin Tarantino rather than Tarantino. So, I can actually change the values afterwards. Furthermore, I can do the same thing in the arrays as well. So, let's go to a one of the arrays.
Let's go to number array here. So, where do we have a number eight here? Here in the myNumberArray. Can I do something like that? My number first elements, I mean the zero. So this is one. If I run this, I will see one on the right-hand side. But what if I want to change this to 15? Can I do that? Of course, I can. If I run this it will show me that my number zero is now 15. And if I call my number array zero one more time over here, then I will see 15. So, this is basically the same thing as coming here and saying myFavoriteDirector is Pulp Fiction is not anymore Tarantino is Quentin Tarantino. So, how do you append the value like we did in the array section? If you do myFavoriteDirectors.append, you will see that there is no method as append or insert or put. You will have to do like this. You will say that myFavoriteDirectors. And let me do Seven Samurai for example.
And the director of the Seven Samurai will be Akira Kurisowa. So, the Seven Samurai is now equal to Akira Kurisowa. So, if I call myFavoriteDirectors one more time. So let me cut this, print and put it over here, and let me run this. And as you can see now I see my value in myFavoriteDirectors dictionary. So, that's how you append a new value and that's how you change the value over here as well. So, this is again a key-value pairing structure. And as I said before of course this wouldn't have to be string to string. You can create something like myDictionary and in this dictionary just suppose that you're building a fitness app. So, running is 100 calories. Swimming is 200 calories and basketball is 300 calories. You can go like this if you just call myDictionary run for example.
So, as you can see even here it's a string to int dictionary. So, I can just say myDictionary run and if I run this I will get 100 here. So, this is good, right? We're going to use dictionaries a lot when we build apps as well as you can see these are not ordered collections like arrays because it doesn't need to be ordered anyway because we're not using indexes over here. We're just calling every value by their keys. So, we have key-value pairings in dictionaries. So, we have learned a lot. So, we have learned about arrays. And if you want to have ordered objects like structured objects, use arrays. If you want to call them by index, use arrays. If you want to have every element uniquely in you're set, in your dataset, then use sets. If you want to have a key-value pairing, then you should go for dictionaries.
And again, we're going to see a lot of examples regarding those structures, and don't worry about it. So, let us stop here. Following the next lecture, we're going to start learning about loops.
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.