0
1
00:00:00,390 --> 00:00:01,860
Hey, guys, it's Angela again.
1

2
00:00:01,860 --> 00:00:04,800
Welcome to another episode on Swift Deep Dive.
2

3
00:00:04,950 --> 00:00:09,250
And in this Deep Dive, we're going to cover Swift Switch Statements.
3

4
00:00:09,390 --> 00:00:15,960
Now, if you were a train driver and you were pulling into a large station, say, Paddington or King's Cross
4

5
00:00:16,050 --> 00:00:21,840
and you had to manually check if every single platform was empty or if it was occupied, that would take
5

6
00:00:21,840 --> 00:00:23,170
you a long time.
6

7
00:00:23,280 --> 00:00:28,860
And that's kind of like trying to write an "if else" statement for a whole bunch of conditions.
7

8
00:00:28,980 --> 00:00:35,240
What's way more efficient and much easier is if you could just ask the station manager which platform
8

9
00:00:35,290 --> 00:00:38,120
should I pull into and they give you a number,
9

10
00:00:38,120 --> 00:00:42,930
1, 2, 3 or 4, and you just go to that platform.
10

11
00:00:42,930 --> 00:00:45,840
So this is kind of like how Switch Statements work.
11

12
00:00:45,840 --> 00:00:52,470
We can have a variable, say, if we think about our Egg Timer, we have this variable called hardness which,
12

13
00:00:52,470 --> 00:00:59,100
of course, depends on the button that the user presses on, and we can check to see what the value of that
13

14
00:00:59,250 --> 00:01:06,090
variable is equal to. If it's equal to "Soft," then we could print (5) or the Soft Time, if it's equal to
14

15
00:01:06,090 --> 00:01:08,980
"Medium," then print (7) "Hard," print 12.
15

16
00:01:09,000 --> 00:01:16,620
So, essentially, what's happening here is we're matching the value of this variable to each of these cases
16

17
00:01:16,770 --> 00:01:23,350
and depending on which one matches, then we're going to perform different actions.
17

18
00:01:23,510 --> 00:01:30,050
And finally, at the end, there is a default statement in case none of our cases match the current value,
18

19
00:01:30,320 --> 00:01:35,690
so usually that's where you might print "Error," where you might say, "None of the values match," or "Hey, you
19

20
00:01:35,690 --> 00:01:41,180
might want to check and see what's going on here," or you could use it like an "else" statement, like a catch
20

21
00:01:41,240 --> 00:01:44,840
all, do this in all other cases.
21

22
00:01:44,840 --> 00:01:50,030
Now, the reason why Switch Statements are so great is because not only do they reduce the amount of code
22

23
00:01:50,030 --> 00:01:56,810
you have to type. Instead of having to type if hardness is equal to "Soft," open brackets, close brackets,
23

24
00:01:57,230 --> 00:02:00,030
if hardness is equal to medium etc..
24

25
00:02:00,050 --> 00:02:06,950
But underlying how a Switch Statement versus how an "if" and "else" statement works, if you have more than 5
25

26
00:02:06,950 --> 00:02:13,250
cases, it's usually more efficient to use a Switch Statement because it performs a search rather than
26

27
00:02:13,250 --> 00:02:19,490
having to manually go through each of the conditions and check them. If you have less than 5 conditions,
27

28
00:02:19,520 --> 00:02:24,370
then it's often quicker and easier to write just some "if" statements.
28

29
00:02:24,440 --> 00:02:30,540
Now, what if you can't match the precise value of a variable?
29

30
00:02:30,560 --> 00:02:36,260
What if you wanted to match a range, say, if hardness was actually a number and we wanted to check if
30

31
00:02:36,260 --> 00:02:40,580
it was between, say, 3 and 5, and then 5 and 7?
31

32
00:02:40,730 --> 00:02:42,500
What would you do in that case?
32

33
00:02:42,530 --> 00:02:48,380
Well, here's where the Swift range operator comes in again. And we saw this already before, we saw that
33

34
00:02:48,380 --> 00:02:55,210
you could use three dots to create a closed range where you're creating a range between "a" and "b."
34

35
00:02:55,220 --> 00:03:01,760
So, for example, we could create a whole bunch of random numbers between 1 and 100, including 1 and 100,
35

36
00:03:02,090 --> 00:03:08,030
then we would use the close range, and then we have the half-open range where we have a less than sign
36

37
00:03:08,030 --> 00:03:08,710
at the end.
37

38
00:03:08,870 --> 00:03:13,850
And this means that we have a range between "a" and "b," but not including "b."
38

39
00:03:13,850 --> 00:03:20,090
And finally, one that you haven't seen yet is something called a one-sided range operator where you leave
39

40
00:03:20,120 --> 00:03:23,780
one of the sides either the "a" side or the "b" side empty.
40

41
00:03:24,050 --> 00:03:32,030
So this means that we're creating a range that is up to and including "b," but it can be any number that's
41

42
00:03:32,090 --> 00:03:34,160
less than "b."
42

43
00:03:34,160 --> 00:03:35,990
So bearing in mind what we've just learned,
43

44
00:03:36,380 --> 00:03:42,200
I want you to challenge yourself and play around with these range operators as well as the Switch Statement
44

45
00:03:42,560 --> 00:03:47,360
by changing our existing loveCalculator that you created using an "if,"
45

46
00:03:47,360 --> 00:03:54,440
"else if," "else" statement into one that uses a Switch Statement only. So have a think about what you need
46

47
00:03:54,440 --> 00:03:57,800
to do, maybe rewind and take a look at the range
47

48
00:03:57,810 --> 00:03:58,580
operators
48

49
00:03:58,730 --> 00:04:04,640
if you need to, or Google for the range operator in the Swift Programming Language book and have a read
49

50
00:04:04,670 --> 00:04:05,330
over there.
50

51
00:04:05,810 --> 00:04:09,420
But essentially, the challenge is to simply adapt your loveCalculator
51

52
00:04:09,440 --> 00:04:12,910
that's using an "if" statement into one that uses a Switch Statement.
52

53
00:04:12,960 --> 00:04:17,040
So pause the video and try and give that a go.
53

54
00:04:17,050 --> 00:04:24,820
So here I've got the code from before where we're using our "if," "else if," and "else" statements to check if each
54

55
00:04:24,820 --> 00:04:30,100
of these conditions are true and perform the correct actions.
55

56
00:04:30,100 --> 00:04:34,040
Now, in this case, we're going to change it to a Switch Statement.
56

57
00:04:34,120 --> 00:04:40,510
And, again, I'm going to use the capability of Xcode to provide code snippet by just starting out with
57

58
00:04:40,510 --> 00:04:47,320
writing the keyword which is, of course, "switch," and I'm gonna hit enter, and then insert a code snippet with
58

59
00:04:47,410 --> 00:04:49,770
these placeholders for me to replace.
59

60
00:04:49,780 --> 00:04:55,100
So, in this case, the value that we're checking or we're switching on is, of course, our loveScore.
60

61
00:04:55,120 --> 00:04:55,950
So that goes there.
61

62
00:04:56,620 --> 00:05:03,720
And then the case or the pattern that we're matching is whether if loveScore is greater than 80.
62

63
00:05:03,880 --> 00:05:08,810
So in this case, I'm going to check if the loveScore is greater than 80.
63

64
00:05:08,950 --> 00:05:15,620
So to do that using the close range operator, we would write 81...100.
64

65
00:05:15,640 --> 00:05:20,730
And in that case, the code that should get executed is, of course, this line, printing
65

66
00:05:20,830 --> 00:05:23,310
"You love each other like Kanye loves Kanye."
66

67
00:05:23,440 --> 00:05:29,830
Now, in order to add another case, I'm simply just going to write the word "case," and then I'm going to
67

68
00:05:29,830 --> 00:05:33,760
provide the case pattern that I'm trying to match.
68

69
00:05:33,760 --> 00:05:41,220
So in this case, we'll need to check if the loveScore is greater than 40, and less than or equal to 80.
69

70
00:05:41,290 --> 00:05:44,550
Let's use the half-open range operator this time, so that
70

71
00:05:44,550 --> 00:05:45,230
I'll write
71

72
00:05:45,230 --> 00:05:47,100
41..
72

73
00:05:47,200 --> 00:05:49,150
less than 81.
73

74
00:05:49,210 --> 00:05:56,860
So in this case, what we should do is to print "You go together like Coke and Mentos." And the last case
74

75
00:05:56,920 --> 00:06:04,720
I'm going to add is if it's between 0 and 40. Let's use the one-sided range operator this time by writing
75

76
00:06:04,810 --> 00:06:07,450
...40.
76

77
00:06:07,510 --> 00:06:11,280
And finally, we're going to print "You'll be forever alone."
77

78
00:06:11,290 --> 00:06:16,190
Now, the default statement should catch if loveScore was ever outside of this range.
78

79
00:06:16,240 --> 00:06:22,660
And because we're creating a random number here between 0 and 100, it's really unlikely that it's ever
79

80
00:06:22,660 --> 00:06:28,150
going to go outside of our three Switch Statements. And if it really doesn't, then we should print that
80

81
00:06:28,180 --> 00:06:32,020
there's an error and love score is out of range.
81

82
00:06:32,530 --> 00:06:34,780
And that's my sample solution to the challenge.
82

83
00:06:35,230 --> 00:06:40,120
Now, your code might be slightly different and you might have used slightly different range operators,
83

84
00:06:40,390 --> 00:06:48,120
but as long as you print the same output as with the "if else" block, then your solution is correct. So
84

85
00:06:48,330 --> 00:06:53,100
here we've created our Switch Statement and it replaces our
85

86
00:06:53,130 --> 00:06:59,310
"if" and "else" statement. Now, in this case, it might seem like we're actually writing a bit more code than before.
86

87
00:06:59,310 --> 00:07:06,220
But if you had a lot of cases, say, 5 or 6 or 7 or more, then it would be a lot more efficient
87

88
00:07:06,240 --> 00:07:13,020
writing it like this, and your code will also run much faster than if you just had a whole bunch of conditions
88

89
00:07:13,440 --> 00:07:17,270
which each need to be checked and confirmed. Instead,
89

90
00:07:17,280 --> 00:07:23,340
this case, we are pattern matching the value of this against one of these cases and it's more performant
90

91
00:07:23,430 --> 00:07:25,520
when there are a lot more conditions.
91

92
00:07:27,510 --> 00:07:33,030
.
92

93
00:07:33,090 --> 00:07:35,340
.
