1 00:00:00,630 --> 00:00:06,100 We've talked about a raise in Swift, which is a collection of similar objects. 2 00:00:06,390 --> 00:00:13,500 Now there's a special sub array, if you like, called a set in Swift and you get it in a few other 3 00:00:13,500 --> 00:00:16,230 languages, but not that many, as I recall. 4 00:00:16,920 --> 00:00:20,970 This is called a set because it is literally a set of items. 5 00:00:21,170 --> 00:00:23,510 Again, they're all the same kind of item. 6 00:00:24,270 --> 00:00:30,180 But if you think about what a set is in real life, a set is a collection of things that doesn't have 7 00:00:30,180 --> 00:00:30,780 a repeat. 8 00:00:31,290 --> 00:00:38,610 So if I have a set of baseball cards, then that set has one of each baseball player of whatever team 9 00:00:38,610 --> 00:00:39,270 I'm targeting. 10 00:00:39,450 --> 00:00:42,010 You wouldn't put two of the same player in the set, would you? 11 00:00:42,600 --> 00:00:44,220 So that's a handy way to remember sets. 12 00:00:44,220 --> 00:00:52,560 And here's an example we have var faves is a set of strings, which is rock, classical and hip hop, 13 00:00:52,560 --> 00:00:54,670 which is obviously musical styles. 14 00:00:55,320 --> 00:00:58,160 What if I want to add a fave into this. 15 00:00:58,170 --> 00:01:03,920 Well, faves dot, if you remember ARray's we used append with this. 16 00:01:03,930 --> 00:01:08,550 It's not so straightforward because we use insert now. 17 00:01:08,550 --> 00:01:16,440 The subtle difference between append and insert is that append add something to the end at the end is 18 00:01:16,440 --> 00:01:24,390 close enough to add end to remember insert just puts something in to the set of items. 19 00:01:25,350 --> 00:01:31,830 If that new something is the same as something that's already in the set, there'll be no change to 20 00:01:31,830 --> 00:01:35,400 the size of it and that same thing will only appear once. 21 00:01:35,790 --> 00:01:36,120 Right. 22 00:01:36,650 --> 00:01:37,890 So I'm going to give you an example. 23 00:01:38,220 --> 00:01:41,920 First of all, of a lowercase rock are. 24 00:01:42,420 --> 00:01:46,290 So if we play this, that set now has four items. 25 00:01:47,890 --> 00:01:49,900 If we print it out, we'll be able to see. 26 00:01:54,560 --> 00:01:58,340 Hip hop, classical rock and lowercase rock. 27 00:01:58,730 --> 00:02:04,630 OK, so lower case is obviously different to upper case as far as Swift is concerned. 28 00:02:05,090 --> 00:02:08,300 In some cases that may not be the case in different languages. 29 00:02:08,310 --> 00:02:09,520 That could be different, too. 30 00:02:10,620 --> 00:02:11,210 OK. 31 00:02:12,230 --> 00:02:13,400 What do we want to do? 32 00:02:13,430 --> 00:02:20,750 Well, we'd like to see if faves contains rock faves that contains. 33 00:02:22,380 --> 00:02:27,720 Rock, and if we hit play on that, that should give us true because it is actually there. 34 00:02:28,140 --> 00:02:30,810 That's how we check if a set contains a value. 35 00:02:31,290 --> 00:02:37,380 Now, if you were doing a stamp collecting app, for example, you would have this would be ideal for 36 00:02:37,380 --> 00:02:41,360 it because you'd have one of each stamp and you'd query your app and say, do I have this one? 37 00:02:41,820 --> 00:02:46,740 You go into the set and say, no, we don't have that one, so please go and collect it. 38 00:02:50,160 --> 00:02:57,660 Now, we can actually mash these things together, but before we do that, I just want to show you. 39 00:02:59,040 --> 00:03:02,790 What happens if I actually try and insert another? 40 00:03:03,920 --> 00:03:07,250 Lower case rock, and then again, I print. 41 00:03:09,380 --> 00:03:10,100 The favs. 42 00:03:14,330 --> 00:03:19,850 Rock still only appears once that's the lowercase version, because, like I said, if it's already 43 00:03:19,850 --> 00:03:21,500 in there, it doesn't care. 44 00:03:21,710 --> 00:03:24,860 It's just going to not insert that new item for you. 45 00:03:26,000 --> 00:03:26,470 Right. 46 00:03:26,780 --> 00:03:30,220 Let's have a var called rubbish. 47 00:03:30,560 --> 00:03:31,170 What is this? 48 00:03:31,190 --> 00:03:33,860 This is, again, a set of string. 49 00:03:35,870 --> 00:03:37,220 Equal to hip hop. 50 00:03:37,430 --> 00:03:41,260 Now, I'm not saying hip hop's rubbish, I just had to do an example for this. 51 00:03:41,570 --> 00:03:43,550 I like hip hop sometimes, right? 52 00:03:44,000 --> 00:03:45,290 Depending on which song, of course. 53 00:03:46,430 --> 00:03:49,490 And of course, I forgot to put my brackets around. 54 00:03:52,160 --> 00:03:57,530 So now we have a second set, you might have a friend who has a set of stamps and you have a set and 55 00:03:57,530 --> 00:04:03,610 you want to merge them and see what full sets you can come out with using that merge. 56 00:04:03,860 --> 00:04:05,590 And of course, it's the same here. 57 00:04:05,900 --> 00:04:08,600 We can have five dot union. 58 00:04:10,000 --> 00:04:11,830 Faves, rather, this should be plural. 59 00:04:15,440 --> 00:04:20,540 Union with rubbish that's hit play and see what that gives us. 60 00:04:22,320 --> 00:04:30,540 Right, so we've added in hip hop or we haven't, because when we tried to unionise, it already exists. 61 00:04:30,780 --> 00:04:33,390 So what I'm going to do is remove it from this top one. 62 00:04:36,030 --> 00:04:42,900 Now, we should get there we go so it doesn't exist in the original anymore, but when we unión them 63 00:04:42,900 --> 00:04:47,750 together, smash them together, we get it existing, what other things do we have? 64 00:04:48,150 --> 00:04:51,030 We have favs dot intersection. 65 00:04:53,640 --> 00:04:59,270 With rubbish, so this is saying if I bring these two together, what are the similar elements? 66 00:04:59,280 --> 00:05:03,680 And in this case it should be an empty set because nothing is similar. 67 00:05:04,470 --> 00:05:05,850 But if I put in. 68 00:05:07,060 --> 00:05:08,230 That hip hop again. 69 00:05:10,850 --> 00:05:17,720 Now, our intersection should give us hip hop and there we have it as part of that set. 70 00:05:19,220 --> 00:05:22,280 We can also do favors that subtracting. 71 00:05:24,550 --> 00:05:30,850 Returns a new set containing the elements of this set that do not occur in the given sequence. 72 00:05:31,360 --> 00:05:38,160 This is like doing it's like doing the intersection, but bringing back the elements that don't intersect. 73 00:05:38,770 --> 00:05:41,800 And then we have favs dot symmetric difference. 74 00:05:41,830 --> 00:05:43,540 I just copy this to save me from. 75 00:05:44,400 --> 00:05:50,600 Actually writing it, so if we read all these, we should see what happens, it does the same thing, 76 00:05:51,390 --> 00:05:54,750 the symmetric difference, I'm not quite sure what the symmetric differences. 77 00:05:54,750 --> 00:05:55,410 Let's have a look. 78 00:05:57,570 --> 00:06:02,790 Returns the new set with the elements that are either in this set or in the given sequence, but not 79 00:06:02,790 --> 00:06:04,440 in both. 80 00:06:04,590 --> 00:06:07,100 OK, interesting, right? 81 00:06:07,410 --> 00:06:08,940 I've never actually used this. 82 00:06:08,940 --> 00:06:11,490 I've used that once or twice in my life. 83 00:06:11,790 --> 00:06:13,740 But since I have actually used. 84 00:06:14,670 --> 00:06:22,770 Writes my notes over here say you get a homework break, but let's remove that, so I guess there's 85 00:06:22,770 --> 00:06:24,080 no homework for this section. 86 00:06:24,450 --> 00:06:28,170 Lucky you and lucky me, because that's one less video to record.