Contents

keyboard_tab

The course is part of this learning path

Start course
Overview
Difficulty
Intermediate
Duration
2h 50m
Students
21
Description

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.

Transcript

Hi. Within this lecture, we're going to learn about sets. So, sets are very similar to arrays over here, but they will be a little bit different. They will have their advantages and disadvantages. So, I am going to take notes again. I am going to label arrays over there and right now I am going to go for sets.

So, we're going to learn about sets and we're going to continue learning about arrays in this lecture as well. So, what is a set? So, this is kind of an array again, but they have unique values inside of them. So, you cannot have two JAMES' inside of a set, you cannot have two 1s inside of a set. So, let me show you how this works out. So, if you do something like this, var mySet and open parentheses again and do 1, 2, 3, 4, 5. And again, swift will think that this is an array rather than a set. Because as you can see, if I do mySet, it will think that this is an integer array. But I don't want this to be an array, I want it to be a set. So, I can predefine this. I can just go for here and say this is a set. So, right now, if I do mySet, as you can see this is a set having integers inside of it. So, what's the difference between sets and arrays? So first, as I said before, you cannot have same value more than one inside of a set. And sets are unordered collections. So, let me show you what I mean. Can I come over here to my number array?

And can I append something? Or can I change something like this? If I do myNumberArray0, I will get 1, obviously right, because this is the first element. And I cannot do that in set, I cannot go over here and just bring me the first element.  Because sets do not have indexes Because sets do not have indexes,  they have something called last or first. They used to have last, but I believe they have got rid of them. So, they have the first. For example, if I run this, I won't even get 1, I will get 3. And every time I run this, this will change because the sets are unordered collections, they do not have indexes like we do in arrays. So, you cannot have sorted sets, you can have sorted arrays. You can create some structural values, you can have some sorted values in arrays.

You can go over here and you can try to apend something like, you can just say append and you can add a new element like 8 over here, and it will be ordered like 1, 2, 3, 4, 5, 6, 7, 8. So, if I call this and hit 'Play' button on the right hand side, I will see that this will be like 1, 2, 3, 4, 5, 6, 7, 8. So, I can just make this a little bit bigger. And as you can see, 8 appended at the end of this array, it got the last index. So, if I say my append at last, it will give me a reasonable result over here like 8. And as you can see, we got the 8, so let me click over this, and just close this down and this one too. And in here, I cannot do that and mySet that first doesn't even make sense.

So, right now, you may think that why are we even using the sets? Why do we have sets? They're not even ordered. So, let me do it in this way. So, 1, 2, 3, 4, 5, 1, 2. If I run mySet, I won't get 1 and 2 twice. I only get them once. But I can add as many as I want in arrays. So, as you call this, you will see that we will see 1, 2, 3, 4, 5, 6, 7, and 1, 2, 3 one more time. As you can see, now this is my array, I can see the duplicate values as well. But in sets, I cannot do that and this is a beneficial sometimes, if I don't want any duplicates inside of an collection then I can use sets. So, sets are unordered collections.

Unordered collections, and they have unique values. So, if you're looking for unordered collections with unique elements, then you have to use sets. For example, I think that you have a string set, in which you will have a, b, and then c, and then a again. So, this is the same principle. If you run this, you won't see a twice, you will only get it once, but it will be unordered. So, for example, let's suppose that you have an array, but inside of this array you have duplicated values. How do you get rid of those values? Of course, you can try to convert your array into a set. Then the problem will be solved. So, let me try this.

So, I am going to say, this is myInternetArray. Let's suppose that I am downloading this data from the Internet and I have something like this,  like set of integers 1, 2, 3, 1, 2, 3. And I have 1 more than three times maybe, 2 more than two times. So, if I try to convert this into a set, I can wrap this around set like I did with strings and integers, I can just convert this array into a set. Now, if I run this, I will see that in my array I have all the values. So, let me print this so we can see it better. So, let me print myInternetArray and let me print myInternetSet.

So, let me run those values. And as you can see, I have everything in myInternetArray, but I don't have the duplicated values inside mySet. So, if you come across a situation in which you have to get rid of the duplicated values, and then you can just do whatever you want to do with those left, then you can convert this structure into a set. So, for example, let me have something called mySet1. And in this set, I have 1, 2, 3.

And again, I have mySet2, and in this set I have 3, 4, 5. Now I want to combine these two sets together to have 1, 2, 3, 4, 5, rather than having 3 two times. How do I do this? I can create mySet3, and I can just say mySet1.union. And as you can see, it asks for an other sequence, and in this case the other sequence is mySet2. So, it means that, just union make a union out of this mySet1 and mySet2. And if you do print mySet3, you will get what?

You will get 1, 2, 3, 4, 5, of course in an unordered fashion. So, we don't have indexes in sets, but we have unique elements. So, whenever you need this, you are more than welcome to use sets. So, we're going to stop here and within the next lecture, we're going to see another array or set like structure called dictionaries.

About the Author
Students
624
Courses
55
Learning Paths
3

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.

Covered Topics