Now that we're all skilled up on functions with inputs, we're finally ready to complete our Xylophone 1
app. 2
Given that we've just learned all about function parameters and function arguments, I'd like to start 3
this lesson with the challenge. 4
Can you modify the play sound function so that it can take a string input to an input of the data type 5
string, and it's gonna be called Sound name, and then use that input instead of this hardcoded C letter, 6
and finally, pass over this sender.CurrentTitle 7
when you call the playSound function so that when you press any of the keys, it will play the corresponding 8
sound? 9
So pause the video and try to complete that challenge. 10
All right. 11
So first things first, let's add an input to this function play sound. 12
Let's modify it. And we said that we were gonna call the input sound name, and then we were gonna give 13
it the data type of a string. 14
So now that we've modified our function, we can now use this soundName inside our function and we're 15
gonna use it to replace this resource name. 16
So instead of using just C every time we want to play a sound, we're gonna use that sound name that gets 17
passed in, 18
and that could be any one of the titles of our buttons. 19
So now when I playSound, I'm going to pass over the soundName. 20
Notice how it's telling me that I'm now missing an argument for the parameter soundName. 21
And if I click on it, I can say Fix. 22
So now it adds the parameter name which is what we specified here and it's expecting a value. 23
Now, that value is not gonna be something I'm going to simply type out D or C, instead it's going to be 24
based on what the sender.currentTitle is. 25
So I'm going to cut it from there and paste it in here, and then I'm going to delete my print statement. 26
Now, the final thing that we actually have to do is we have to click on this and simply click Fix on 27
one of these options. 28
Now, the one that I'm going to choose is an exclamation mark. 29
And the reason why we need it and why we get that error is because the sender.currentTitle has 30
a data type that is something that we would call an optional string, 31
so the optional comes from the question mark. 32
And it's because that we could have a button whose title is actually nil, it doesn't exist, in which case, 33
we would be playing around with some nonexistent value and we'd be trying to pass it over to our 34
playSound function, and then we would try to play a nonexistent value, and then our app will just come tumbling 35
down. 36
So the reason why this is an optional 37
and it forces you to put in an exclamation mark is because it forces you to check and think, "Is there 38
a situation where one of my buttons might not have a title?" And we can see that out of all of the buttons 39
that's linked to our IBAction, 40
they all have a title. 41
So there's really no case where this can fail. 42
So we can safely add an exclamation mark to say, don't worry, we've checked. It's always going to have 43
a title, 44
so we'll just send it over as the input to our playSound function. 45
So now if I click on the C button, sender.currentTitle will be equal to C, and then C becomes the 46
sound name, and C gets put in here, 47
so then it plays C.wav, and it will change that depending on which button I press. 48
So now let's run our app. And if I go ahead and click... 49
Notice how every single key now has a different sound. 50
And we've now completed our Xylophone app using a bit of knowledge about StackOverflow, about Google 51
Search, as well as the Apple Documentation iOS Programming. 52
So run your app and play the xylophone to your heart's content. 53
And once you're done, we're going to tackle a boss challenge.