0
1
00:00:00,090 --> 00:00:02,610
So how did you get on with the challenge?
1

2
00:00:02,610 --> 00:00:06,130
Well, there's, of course, quite a few ways of solving this challenge.
2

3
00:00:06,330 --> 00:00:14,340
And one of the simplest is by using some "if" and "else" statements, so we could check to see if the hardness
3

4
00:00:14,820 --> 00:00:21,540
that the user selected, which is captured in our constant here, is equal to Soft.
4

5
00:00:21,540 --> 00:00:28,200
So remember, this is going to correspond to the title of the button that was pressed and our buttons
5

6
00:00:28,290 --> 00:00:33,740
are named Soft, medium, and hard with a capitalized first letter.
6

7
00:00:33,810 --> 00:00:36,890
So if the hardness was Soft,
7

8
00:00:36,930 --> 00:00:44,990
so if the Soft button was pressed, then we probably want to print the softTime. Now,
8

9
00:00:45,000 --> 00:00:53,340
else if the hardness that was selected was equal to "Medium" with a capital M, then, in that case, we should
9

10
00:00:53,340 --> 00:00:56,140
probably print the mediumTime.
10

11
00:00:56,280 --> 00:01:03,040
And finally, we have an "else" statement that captures the last condition because there are only three buttons
11

12
00:01:03,040 --> 00:01:08,530
that can only be the Hard button that got pressed, so print the hardTime.
12

13
00:01:08,610 --> 00:01:14,070
Now, an alternative to this is we could have also used a Swift Switch Statement,
13

14
00:01:15,330 --> 00:01:19,650
so we could have switched on our hardness value.
14

15
00:01:19,650 --> 00:01:25,440
So this string and if the case was that it was equal to soft.
15

16
00:01:25,440 --> 00:01:34,080
Well, in that case, we should print the softTime. And if the case was equal to Medium, then we could print
16

17
00:01:34,440 --> 00:01:43,800
the mediumTime. And either through the use of the default which, of course, we can only have three options,
17

18
00:01:43,890 --> 00:01:47,770
we could print our hardTime.
18

19
00:01:47,820 --> 00:01:54,180
Alternatively, we could add another case and make the default catch exceptions where for some reason
19

20
00:01:54,180 --> 00:02:00,210
or other, we have a button that's triggering this IBAction whose title is not equal to Soft, Medium,
20

21
00:02:00,270 --> 00:02:01,260
or Hard.
21

22
00:02:01,260 --> 00:02:04,810
So in that case, we could add another case "Hard,"
22

23
00:02:05,100 --> 00:02:07,840
and this one would print hardTime
23

24
00:02:07,920 --> 00:02:10,860
and this one would print "Error."
24

25
00:02:10,860 --> 00:02:12,780
So both of these work.
25

26
00:02:12,810 --> 00:02:18,690
And if you used either of these solutions, then consider yourself successful and you solve the challenge.
26

27
00:02:18,690 --> 00:02:26,910
But there's actually an even shorter way of performing this action. Instead of using these three constants
27

28
00:02:27,060 --> 00:02:30,500
where they're just holding a single piece of data,
28

29
00:02:30,570 --> 00:02:35,910
we could also just capture them all in a single dictionary.
29

30
00:02:35,910 --> 00:02:39,960
So let's create a dictionary and I'm going to create the dictionary as a constant.
30

31
00:02:39,960 --> 00:02:44,430
So using the "let" and then I'm going to call it eggTimes.
31

32
00:02:44,460 --> 00:02:52,680
So this is the name of my dictionary. And then, I'm going to set it to equal a set of square brackets.
32

33
00:02:52,680 --> 00:02:58,350
Now, this might seem familiar to you because we've already used square brackets when we were learning
33

34
00:02:58,350 --> 00:03:03,200
about arrays. But there is another type of collection that we could use
34

35
00:03:03,390 --> 00:03:08,310
and it's a dictionary. Similar to a real-world dictionary,
35

36
00:03:08,310 --> 00:03:09,930
you have a key.
36

37
00:03:09,960 --> 00:03:15,660
So, for example, a word that you were looking up in the dictionary, and let's say that word was "Soft," and
37

38
00:03:15,660 --> 00:03:18,850
then you have a corresponding matching value.
38

39
00:03:18,900 --> 00:03:27,240
So for the "Soft" word, in our case, the value is going to be equal to 5. For the medium string,
39

40
00:03:27,240 --> 00:03:33,090
well, our value is going to be 7. And for the Hard string of value is going to be 12.
40

41
00:03:33,510 --> 00:03:37,380
So we've basically done exactly the same thing as what we had before,
41

42
00:03:37,380 --> 00:03:40,520
softTime, mediumTime, and hardTime,
42

43
00:03:40,710 --> 00:03:44,770
but now we have it captured in a single dictionary.
43

44
00:03:44,880 --> 00:03:51,690
And at this point, I want to post another challenge for you. If you have seen dictionaries before in other
44

45
00:03:51,690 --> 00:03:57,870
programming languages or if you already know about it from Swift, then you should be able to use this
45

46
00:03:57,990 --> 00:04:04,390
eggTime dictionary to print out the correct time for the hardness that's selected,
46

47
00:04:04,590 --> 00:04:08,790
so we know that we can capture the title of the button that got pressed.
47

48
00:04:08,790 --> 00:04:15,510
I want you to replace this switch statement or the "if" statement that you currently have with a way of
48

49
00:04:15,510 --> 00:04:22,380
pulling out the values from our dictionary based on the keys we've got here.
49

50
00:04:22,440 --> 00:04:28,490
This challenge will require you to know a little bit about both dictionaries and optionals.
50

51
00:04:28,500 --> 00:04:33,570
So if you know how to complete this challenge and you've managed to use the dictionary to print out
51

52
00:04:33,570 --> 00:04:40,410
the correct time for the button that's selected, then feel free to skip the next lesson on our Swift
52

53
00:04:40,500 --> 00:04:41,490
Deep Dive.
53

54
00:04:41,490 --> 00:04:45,780
But if you don't know how to complete this challenge or if you've never seen dictionaries, you want to
54

55
00:04:45,780 --> 00:04:50,730
learn more about Swift Dictionaries, then head over to the next lesson where we're going to do a Deep
55

56
00:04:50,730 --> 00:04:52,620
Dive on Swift Dictionaries.
