1 00:00:00,150 --> 00:00:06,330 Sometimes when you're coding, you want to select from a list of values, you want to assign a list 2 00:00:06,330 --> 00:00:08,610 of values to things that you're pulling from a server. 3 00:00:09,240 --> 00:00:13,140 Now, the naive way to do that is to assign some strings. 4 00:00:13,290 --> 00:00:17,040 So if you have music, you'll have music if you have podcast podcasts. 5 00:00:17,670 --> 00:00:18,930 But that's not the way we do things. 6 00:00:19,500 --> 00:00:23,280 What we need to use for that is called an enumeration enum for short. 7 00:00:25,290 --> 00:00:28,530 A good example of this is a compass point. 8 00:00:28,620 --> 00:00:33,540 So this is how we define an enum and the and enum takes cases. 9 00:00:34,490 --> 00:00:38,030 So we have a case for North Whips, you know why it's done that? 10 00:00:39,510 --> 00:00:41,190 We have a case for South. 11 00:00:44,740 --> 00:00:47,110 And we have a case. 12 00:00:49,130 --> 00:00:52,070 For West, the case for east. 13 00:00:53,870 --> 00:00:56,400 Right, so now we have an enemy. 14 00:00:56,990 --> 00:00:58,010 How do we use it? 15 00:00:58,400 --> 00:01:03,680 Well, let's say we have some kind of variable and we want to set it to one of these options will say 16 00:01:03,680 --> 00:01:05,060 for a equals. 17 00:01:06,200 --> 00:01:07,070 Compass points. 18 00:01:08,200 --> 00:01:09,550 Don't know if. 19 00:01:10,640 --> 00:01:15,920 And that is it, the reason we do this, and you may think it's a bit weird that we do this if you're 20 00:01:15,920 --> 00:01:20,810 just learning coding, but the reason we do it is so we have a central location like we should with 21 00:01:20,810 --> 00:01:27,140 all code where our values are stored and there are a finite number of options. 22 00:01:27,620 --> 00:01:32,510 So if I come along to Compass Point North, I know what I'm talking about. 23 00:01:32,900 --> 00:01:34,580 And if I wanted to change that. 24 00:01:40,950 --> 00:01:47,360 I could do the following, and that should change it everywhere else if we refactor the whole thing. 25 00:01:47,700 --> 00:01:51,370 So we have a single central location where we define these things. 26 00:01:52,020 --> 00:01:56,400 Now, the other way we can define an enum is to have. 27 00:01:57,490 --> 00:01:59,020 That's a compass point out. 28 00:02:01,850 --> 00:02:05,690 Yes, curly brace, thank you, and we will have case. 29 00:02:06,690 --> 00:02:07,890 North, south. 30 00:02:08,610 --> 00:02:13,040 East, west, and we just put commas between them, so I haven't typed out the whole thing here. 31 00:02:13,200 --> 00:02:16,260 You don't want to go through a typing class de. 32 00:02:17,650 --> 00:02:19,840 OK, so that's the other way we can define this. 33 00:02:20,830 --> 00:02:25,150 We don't have to type each one out just nice and compact, depending on how many cases you have, of 34 00:02:25,150 --> 00:02:25,510 course. 35 00:02:26,410 --> 00:02:26,800 Right. 36 00:02:26,800 --> 00:02:30,890 But I would say, you know, with code, don't try and save lines unnecessarily. 37 00:02:31,090 --> 00:02:35,020 I actually prefer this first version because it makes it really easy to read. 38 00:02:36,480 --> 00:02:37,200 So that's that. 39 00:02:37,680 --> 00:02:38,850 So what else have I got here? 40 00:02:43,350 --> 00:02:50,070 I've got this Inam, which comes from the Swift Example's website, the Aski Control Character, which 41 00:02:50,070 --> 00:02:56,850 is, of course a character type so we can assign values to our enemies. 42 00:02:57,280 --> 00:03:02,880 So when I have a case of tab, I can have backslash t line feeds backslash in which you've seen in the 43 00:03:02,880 --> 00:03:06,030 print statement and the return is backslash. 44 00:03:06,030 --> 00:03:13,390 Are all of these things on all OSes will generally go on to the next line line feed and carriage return 45 00:03:13,410 --> 00:03:18,000 tab will advance forward by a set number of spaces. 46 00:03:19,410 --> 00:03:22,710 OK, so that's how we assign variables into things. 47 00:03:22,740 --> 00:03:28,470 Now, if I was going to use that some way, I would have something like this to tap. 48 00:03:29,100 --> 00:03:33,370 Now I know wherever I use that, I'm getting this character. 49 00:03:34,260 --> 00:03:40,890 So in the future, if that character changes to forward slash something, I change it in one place and 50 00:03:40,890 --> 00:03:42,300 it gets carried through everywhere. 51 00:03:43,360 --> 00:03:44,570 Forward slash is a bad idea. 52 00:03:45,250 --> 00:03:45,640 OK. 53 00:03:47,990 --> 00:03:49,430 Oh, because I've got characters. 54 00:03:50,150 --> 00:03:53,510 Yeah, if it was a string, then I could change it. 55 00:03:53,510 --> 00:03:58,580 But if the character thing changes from T to P, then that gets changed everywhere. 56 00:03:59,000 --> 00:04:01,960 So I've only got one place to keep it. 57 00:04:02,270 --> 00:04:07,820 And in fact, this is a terrible example because it keeps giving me errors because the backslash t is 58 00:04:07,820 --> 00:04:09,440 a specific ASCII character. 59 00:04:09,470 --> 00:04:13,250 But anyway, let's let's leave that aside for one minute. 60 00:04:13,250 --> 00:04:15,020 Let's pretend that I actually got it right. 61 00:04:16,010 --> 00:04:20,870 The point is, if I change it once here, it changes throughout the application. 62 00:04:20,870 --> 00:04:24,520 And that's the way you should always construct your code. 63 00:04:25,370 --> 00:04:25,540 Right. 64 00:04:25,610 --> 00:04:26,710 So here's a bit of homework for you. 65 00:04:26,720 --> 00:04:27,650 It's really easy. 66 00:04:28,680 --> 00:04:29,750 We're making a music map. 67 00:04:29,940 --> 00:04:36,200 Our server returns a list of music, podcasts or albums, make an enum to represent those. 68 00:04:37,280 --> 00:04:38,050 Nice and simple.