0
1
00:00:00,770 --> 00:00:06,110
Now that we're all skilled up on functions with inputs, we're finally ready to complete our Xylophone
1

2
00:00:06,170 --> 00:00:06,990
app.
2

3
00:00:07,190 --> 00:00:12,500
Given that we've just learned all about function parameters and function arguments, I'd like to start
3

4
00:00:12,500 --> 00:00:14,060
this lesson with the challenge.
4

5
00:00:14,120 --> 00:00:22,220
Can you modify the play sound function so that it can take a string input to an input of the data type
5

6
00:00:22,520 --> 00:00:31,220
string, and it's gonna be called Sound name, and then use that input instead of this hardcoded C letter,
6

7
00:00:32,030 --> 00:00:36,090
and finally, pass over this sender.CurrentTitle
7

8
00:00:36,230 --> 00:00:44,330
when you call the playSound function so that when you press any of the keys, it will play the corresponding
8

9
00:00:44,330 --> 00:00:45,820
sound?
9

10
00:00:45,830 --> 00:00:49,280
So pause the video and try to complete that challenge.
10

11
00:00:52,140 --> 00:00:52,490
All right.
11

12
00:00:52,520 --> 00:00:56,830
So first things first, let's add an input to this function play sound.
12

13
00:00:56,840 --> 00:01:03,530
Let's modify it. And we said that we were gonna call the input sound name, and then we were gonna give
13

14
00:01:03,530 --> 00:01:06,350
it the data type of a string.
14

15
00:01:06,380 --> 00:01:14,060
So now that we've modified our function, we can now use this soundName inside our function and we're
15

16
00:01:14,060 --> 00:01:17,300
gonna use it to replace this resource name.
16

17
00:01:17,390 --> 00:01:23,960
So instead of using just C every time we want to play a sound, we're gonna use that sound name that gets
17

18
00:01:23,960 --> 00:01:24,590
passed in,
18

19
00:01:24,980 --> 00:01:30,210
and that could be any one of the titles of our buttons.
19

20
00:01:30,230 --> 00:01:36,380
So now when I playSound, I'm going to pass over the soundName.
20

21
00:01:36,440 --> 00:01:42,100
Notice how it's telling me that I'm now missing an argument for the parameter soundName.
21

22
00:01:42,240 --> 00:01:44,900
And if I click on it, I can say Fix.
22

23
00:01:44,930 --> 00:01:51,740
So now it adds the parameter name which is what we specified here and it's expecting a value.
23

24
00:01:51,740 --> 00:01:59,300
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

25
00:01:59,300 --> 00:02:02,660
based on what the sender.currentTitle is.
25

26
00:02:02,660 --> 00:02:08,540
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

27
00:02:08,540 --> 00:02:16,550
Now, the final thing that we actually have to do is we have to click on this and simply click Fix on
27

28
00:02:16,640 --> 00:02:18,590
one of these options.
28

29
00:02:18,590 --> 00:02:22,540
Now, the one that I'm going to choose is an exclamation mark.
29

30
00:02:22,610 --> 00:02:29,900
And the reason why we need it and why we get that error is because the sender.currentTitle has
30

31
00:02:30,020 --> 00:02:34,790
a data type that is something that we would call an optional string,
31

32
00:02:34,790 --> 00:02:37,210
so the optional comes from the question mark.
32

33
00:02:37,760 --> 00:02:45,650
And it's because that we could have a button whose title is actually nil, it doesn't exist, in which case,
33

34
00:02:45,740 --> 00:02:52,050
we would be playing around with some nonexistent value and we'd be trying to pass it over to our
34

35
00:02:52,050 --> 00:02:58,820
playSound function, and then we would try to play a nonexistent value, and then our app will just come tumbling
35

36
00:02:58,820 --> 00:02:59,650
down.
36

37
00:02:59,660 --> 00:03:02,300
So the reason why this is an optional
37

38
00:03:02,390 --> 00:03:09,410
and it forces you to put in an exclamation mark is because it forces you to check and think, "Is there
38

39
00:03:09,410 --> 00:03:16,660
a situation where one of my buttons might not have a title?" And we can see that out of all of the buttons
39

40
00:03:16,660 --> 00:03:18,440
that's linked to our IBAction,
40

41
00:03:18,470 --> 00:03:20,460
they all have a title.
41

42
00:03:20,540 --> 00:03:23,400
So there's really no case where this can fail.
42

43
00:03:23,510 --> 00:03:29,720
So we can safely add an exclamation mark to say, don't worry, we've checked. It's always going to have
43

44
00:03:29,720 --> 00:03:30,370
a title,
44

45
00:03:30,410 --> 00:03:35,510
so we'll just send it over as the input to our playSound function.
45

46
00:03:35,510 --> 00:03:43,580
So now if I click on the C button, sender.currentTitle will be equal to C, and then C becomes the
46

47
00:03:43,580 --> 00:03:47,010
sound name, and C gets put in here,
47

48
00:03:47,030 --> 00:03:53,390
so then it plays C.wav, and it will change that depending on which button I press.
48

49
00:03:53,390 --> 00:03:58,040
So now let's run our app. And if I go ahead and click...
49

50
00:04:03,890 --> 00:04:07,480
Notice how every single key now has a different sound.
50

51
00:04:07,610 --> 00:04:14,690
And we've now completed our Xylophone app using a bit of knowledge about StackOverflow, about Google
51

52
00:04:14,690 --> 00:04:19,240
Search, as well as the Apple Documentation iOS Programming.
52

53
00:04:19,460 --> 00:04:23,630
So run your app and play the xylophone to your heart's content.
53

54
00:04:23,840 --> 00:04:27,890
And once you're done, we're going to tackle a boss challenge.
