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
We're almost there! Now that we have a Page containing a Story segment, let's combine them in different ways to form an adventure.
-
0:00
We have the objects we need to create a story for the app.
-
0:03
But we don't want to create the story in our View Controller
-
0:06
every time we initialize a new page of the app, because, for
-
0:11
starters, that's not the View Controller's job.
-
0:14
Let's create a simple wrapper struct that lets us construct a story.
-
0:19
We'll then use this wrapper object to initialize the View Controller.
-
0:23
Because we're giving the View Controller data to work with
-
0:26
rather than creating the data inside the view controller,
-
0:30
we're abstracting the data from the presentation.
-
0:33
The View Controller doesn't care where the data comes from.
-
0:36
It just needs to come in a certain format.
-
0:39
So we can do this in-page, or you can create a new file, and
-
0:44
I'll just do this in-page.
-
0:46
At the bottom, we're going to call our new struct Adventure.
-
0:51
This struct will have a single static computed property that returns the story
-
0:56
we're going to use in the app.
-
0:58
Because a story is many pages linked together, all this computed
-
1:03
property needs to return is the top level page that contains all the linked pages.
-
1:09
This kind of structure where object contains a reference to the next instance
-
1:14
of the object is called a linked list.
-
1:17
In a linked list, an element or node in the list
-
1:21
contains some sort of value it is storing, along with a reference to the next node.
-
1:26
A linked list offers similar functionality as an array, and
-
1:30
depending on your use case, can have certain advantages.
-
1:33
In an array, every element is sequentially stored in a data structure.
-
1:38
If you are constantly removing or adding elements to the array, and
-
1:42
your array is really large, this is a computationally expensive operation.
-
1:48
When an arbitrary element is removed, every single element in the array
-
1:52
after that element has to be shifted one position to maintain the array.
-
1:57
With a linked list, however, because every node simply contains a reference to each
-
2:03
node, inserting one in any position simply means rearranging the node connections.
-
2:08
There's a lot more to linked lists than this, though.
-
2:11
The example we're looking at here is called a singly linked list.
-
2:15
Because each element links to one other element, it is a single linked list.
-
2:20
Now, in a doubly linked list, each element references the next,
-
2:24
as well as a previous element.
-
2:26
Our data structure, although it looks kinda like a linked list,
-
2:29
is not really one.
-
2:31
If we had only one choice, then, yes, we start to resemble a singly linked list.
-
2:36
But our data structure is more like a tree.
-
2:39
Each page connects to two pages,
-
2:41
which can connect to two more pages until the end of time.
-
2:45
So what's the point of all this?
-
2:47
There are many ways to represent data in code.
-
2:50
We use objects to encapsulate data, but, so far,
-
2:53
we've mostly stored this data in arrays or dictionaries.
-
2:57
The page class is the first structure that we've created
-
3:01
that allows us to store many levels of data outside of an array or dictionary.
-
3:05
We're not going to spend much time on data structures here.
-
3:09
That is an entirely new topic altogether.
-
3:11
But we will talk about them as they come up.
-
3:14
In the meantime,
-
3:15
I've put a few links together that you should definitely check out.
-
3:18
Even though you are here to learn iOS development, knowing about the larger
-
3:22
world of computer science and software engineering is quite important.
-
3:27
Okay, so back to our property.
-
3:29
Since page contains a tree of pages forming a story, all we need to return
-
3:33
is the top of the tree, a single page instance that links everything else.
-
3:37
So in Adventure,
-
3:38
we'll create a static property named story that returns the top level page.
-
3:45
Inside the body, let's construct our story using pages, so
-
3:48
you can finally see how neatly our data model comes together.
-
3:52
Let's start with the very top level of our story, the introduction.
-
3:56
We'll say, let returnTrip, we'll create our first page,
-
4:01
= Page, and we'll initialize it with the story.
-
4:06
That is the returnTrip segment of ARINA.
-
4:10
We have an error here.
-
4:11
What is this?
-
4:13
Says Expected pattern, we'll ignore this for now.
-
4:15
Now, because this is the top level of the page, this is the very first page, and
-
4:20
it's going to link to every other page, we can go ahead return this one.
-
4:23
So we'll say return returnTrip, and now we, basically,
-
4:27
have our Page that we want to return.
-
4:30
Let's see if we can get rid of this error.
-
4:32
Okay, there we go.
-
4:34
Now that we have our main page, let's connect the rest of the story together.
-
4:38
So this is the very first page, and every page has two choices.
-
4:41
So next we want to add two choices for the user to continue down.
-
4:44
So we'll say returnTrip.addChoiceWith.
-
4:48
We'll use the method that takes the story.
-
4:52
And for the title we'll say, "Stop and
-
4:55
Investigate" and the story segment is touchDown.
-
5:00
So to returnTrip here,
-
5:01
we're going to use the addChoiceWith method that takes a title and a story.
-
5:06
And we pass in an appropriate title and
-
5:08
the next part of the story based on the choice that we want to link to.
-
5:12
Remember that the story we pass in creates another page,
-
5:15
and our method returns this page.
-
5:18
So we can assign this page to another constant.
-
5:20
So here we can say, let touchDown,
-
5:22
because that's the segment of the story we're connecting to, = returnTrip.
-
5:29
Okay, now we have two pages.
-
5:32
So far, our story looks like this.
-
5:35
Let's add another choice to returnTrip, since each page can contain two choices.
-
5:39
So here we'll say, let homeward, because remember,
-
5:42
adding a choice returns that new page.
-
5:45
And we'll add this again to returnTrip, and the title
-
5:50
here is "Continue home to Earth" and the story is homeward.
-
5:56
Okay, now our tree looks like this.
-
6:00
Since each time we added a choice, we returned and assigned
-
6:04
the new page to a constant, we can now add choices to those new pages as well.
-
6:09
So let's add two more choices to the page containing the touchDown story.
-
6:14
So let's say, let rover = touchdown.addChoiceWith,
-
6:21
"Explore the Rover" and the story is .rover.
-
6:27
We'll also add one more,
-
6:30
let crate = touchdown.addChoiceWith,
-
6:35
"Open the Crate" and the story is crate.
-
6:42
Now our growing tree looks like this.
-
6:44
In this way, we can add the rest of the story in, which I'm going to copy/paste.
-
6:48
And, again,
-
6:49
you can grab the rest of the code from the link in the teacher's notes section.
-
6:54
Again, since the top level page returnTrip now contains the entire story,
-
6:59
since we're linking all these pages together,
-
7:01
we can simply return returnTrip at the end, and now we have our full story.
-
7:06
Before we conclude our model-building activities, I wanna point something out.
-
7:11
When we learned about value types, reference types, and
-
7:14
some of their semantics, we learned that creating a struct that contains reference
-
7:18
types can lead to some weird behavior.
-
7:20
But then I went right ahead and
-
7:22
created a struct called Adventure that returns a page that links to other pages.
-
7:26
And these are reference types.
-
7:29
And this case,
-
7:29
it's okay, because we're creating a static read-only computed property.
-
7:34
We can't mess with the underlying reference types over here and
-
7:38
mutate the data inside of the Adventure struct.
-
7:41
Now, we can do that once we retrieve the instance of page it returns, but
-
7:45
that is a different issue altogether.
-
7:47
Okay, onto the next video.
You need to sign up for Treehouse in order to download course files.
Sign up