So how did you get on with the challenge? 1
Well, there's, of course, quite a few ways of solving this challenge. 2
And one of the simplest is by using some "if" and "else" statements, so we could check to see if the hardness 3
that the user selected, which is captured in our constant here, is equal to Soft. 4
So remember, this is going to correspond to the title of the button that was pressed and our buttons 5
are named Soft, medium, and hard with a capitalized first letter. 6
So if the hardness was Soft, 7
so if the Soft button was pressed, then we probably want to print the softTime. Now, 8
else if the hardness that was selected was equal to "Medium" with a capital M, then, in that case, we should 9
probably print the mediumTime. 10
And finally, we have an "else" statement that captures the last condition because there are only three buttons 11
that can only be the Hard button that got pressed, so print the hardTime. 12
Now, an alternative to this is we could have also used a Swift Switch Statement, 13
so we could have switched on our hardness value. 14
So this string and if the case was that it was equal to soft. 15
Well, in that case, we should print the softTime. And if the case was equal to Medium, then we could print 16
the mediumTime. And either through the use of the default which, of course, we can only have three options, 17
we could print our hardTime. 18
Alternatively, we could add another case and make the default catch exceptions where for some reason 19
or other, we have a button that's triggering this IBAction whose title is not equal to Soft, Medium, 20
or Hard. 21
So in that case, we could add another case "Hard," 22
and this one would print hardTime 23
and this one would print "Error." 24
So both of these work. 25
And if you used either of these solutions, then consider yourself successful and you solve the challenge. 26
But there's actually an even shorter way of performing this action. Instead of using these three constants 27
where they're just holding a single piece of data, 28
we could also just capture them all in a single dictionary. 29
So let's create a dictionary and I'm going to create the dictionary as a constant. 30
So using the "let" and then I'm going to call it eggTimes. 31
So this is the name of my dictionary. And then, I'm going to set it to equal a set of square brackets. 32
Now, this might seem familiar to you because we've already used square brackets when we were learning 33
about arrays. But there is another type of collection that we could use 34
and it's a dictionary. Similar to a real-world dictionary, 35
you have a key. 36
So, for example, a word that you were looking up in the dictionary, and let's say that word was "Soft," and 37
then you have a corresponding matching value. 38
So for the "Soft" word, in our case, the value is going to be equal to 5. For the medium string, 39
well, our value is going to be 7. And for the Hard string of value is going to be 12. 40
So we've basically done exactly the same thing as what we had before, 41
softTime, mediumTime, and hardTime, 42
but now we have it captured in a single dictionary. 43
And at this point, I want to post another challenge for you. If you have seen dictionaries before in other 44
programming languages or if you already know about it from Swift, then you should be able to use this 45
eggTime dictionary to print out the correct time for the hardness that's selected, 46
so we know that we can capture the title of the button that got pressed. 47
I want you to replace this switch statement or the "if" statement that you currently have with a way of 48
pulling out the values from our dictionary based on the keys we've got here. 49
This challenge will require you to know a little bit about both dictionaries and optionals. 50
So if you know how to complete this challenge and you've managed to use the dictionary to print out 51
the correct time for the button that's selected, then feel free to skip the next lesson on our Swift 52
Deep Dive. 53
But if you don't know how to complete this challenge or if you've never seen dictionaries, you want to 54
learn more about Swift Dictionaries, then head over to the next lesson where we're going to do a Deep 55
Dive on Swift Dictionaries.