Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
To allow users to customize the story and add their name we need to add associated values to our enum.
-
0:00
[MUSIC]
-
0:02
[SOUND] Our app is feature complete and we've learned a lot of new things so far.
-
0:08
We learned how to create an app with multiple views,
-
0:11
which included learning about a navigation controller and its navigation stack.
-
0:16
We also learned about creating a complex tree-like model how to inject data into
-
0:21
a view controller how to create and layout views programatically and much more.
-
0:27
These are all foundational topics but now let's have a bit of fun.
-
0:31
The story in our app is always the same regardless of who uses the app.
-
0:35
So let's try and give it a bit of a personal feel.
-
0:38
Let's modifying the app to allow the user to
-
0:41
enter their name before they start the adventure.
-
0:44
Once we navigate through the story will display their name in a few places to make
-
0:48
it feel more personal.
-
0:49
So the first thing we're going to do is head over
-
0:54
to the story.swift file to the story enum.
-
0:57
We're only going to modify our story in minor ways.
-
1:01
Since our model is an enum and each segment of the story is an enum member,
-
1:05
let's give three enum members, just for the sake of example,
-
1:09
we'll give three enum members associated values.
-
1:12
So let's add an associated value of a string to return trip.
-
1:17
So this is going to be an associative value of type string and
-
1:21
will give this argument a name of name.
-
1:24
We'll do the same thing with rover name of type string and with droid.
-
1:31
So again name type string.
-
1:33
We'll pass in the user's name as an associative value to use in our story.
-
1:39
So in the text computed property which should be down here.
-
1:44
Let's bind the associated value to a local constant.
-
1:47
For example we can change case.returnTrip to case.returnTrip (let name).
-
1:54
And now we can use this name to modify the text for this in a member.
-
1:59
So right here where it says help me, you're my only hope,
-
2:02
we can do something like help me.
-
2:04
And then we'll insert the name.
-
2:06
You're my only hope.
-
2:08
Similarly let's add a change to the rover text property right here.
-
2:13
So again we'll bind the name to a local constant and
-
2:17
we'll say there's a message on the screen, and so
-
2:19
it's just saying come we'll say name coma, come to these coordinates.
-
2:26
Finally let's make the same change to the droid member which is the second last one.
-
2:31
Again, we want to bind the name and we'll change the droids name in
-
2:35
the story since the Droid S1124 will now say name S1124,
-
2:40
with these changes we're going to have an error and
-
2:43
that's because we need to make a change to our wrapper struct adventure.
-
2:49
So let's head over to Page.swift.
-
2:52
And in the adventure struct, since two enum members require associated values
-
2:57
now, we need to provide them when creating our story.
-
3:00
Instead of hard coding a name in our story, let's change this from a static
-
3:04
computed property to a static method that takes a parameter.
-
3:09
So instead of static var story, we'll say static func story(withName
-
3:16
name: type String).
-
3:20
And this has a return value of page.
-
3:24
So now the body of the method should stay more or
-
3:25
less the same except, we need to add the associated value in the right places.
-
3:31
So returnTrip takes a name, and we're going to pass through the argument that we
-
3:35
provide in the function, which is name.
-
3:37
And we'll do the same thing down here for rover.
-
3:42
So name, oops.
-
3:45
Rover, and in parentheses, name name.
-
3:49
And finally for droid.
-
3:51
And that is right down here.
-
3:57
And that should clear all our errors out.
-
4:00
Okay, much better.
You need to sign up for Treehouse in order to download course files.
Sign up