Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build an Interactive Story App with Swift Creating a Story Helper Methods

Osama Rehman
Osama Rehman
1,098 Points

Story App

Can someone PLEASE explain a bit more in detail what is going on in the "Story App" video (11:04). What does the code written in the Page.swift mean.

Good luck. I am going to have to start over from the beginning. I must be tired.

2 Answers

I just went through this tutorial and had to relook at this also.

Going back and re-watching Pasan's 1st video (Pages and Stories) is probably necessary to really understand what's going on. The way we're setting up the Page/Story structure is based on the high level explanation there. If you don't understand that part, then it's really hard to follow what the code in Page.swift means.

To recap, here are the structures in this app:

  1. StoryData (enum). In the video, this is named Story, but I'm calling it StoryData to make it clear the only thing it does is to store the story text&image.
  2. Page (class). We set up this class so that it uses the Story Data to create a Page. Pages are created so that you can have an object for each page in the story. Each Page can also have additional Pages attached as either firstChoice or secondChoice.
  3. Adventure (struct). The actual set up and choices for the Pages is done in the Adventure struct. We do this by creating a Page and attaching choices to them (so it ends up like a tree, since each time you attach a choice using the func, another Page is created). Kind of like piecing together a book, page by page.

For example, the code under the Adventure struct... We create the 1st page using the init method, initializing with StoryData enum.

let returnTrip = Page(story: .returnTrip)

And then, we start adding more Pages/choices... using the func addChoice (part of the Page class) on the 1st page we had just created.

let touchdown = returnTrip.addChoiceWith(title: "Stop and Investigate", story: .touchDown)

In the above 2 cases, both returnTrip and touchDown are instances of Page. Basically, the code for the Adventure struct in Page.swift is for setting up the Pages as well as the structure/order of the story.

Yeah, I think it's a bit complicated! Your question is kind of general, so I hope this helped.

Dhanish Gajjar
Dhanish Gajjar
20,185 Points

I think Osama quit swift development. But it is a bit difficult to understand at first. I am re visiting the iOS videos again.

Gavin Hobbs
Gavin Hobbs
5,205 Points

anglin Thank you so much! This helped me finally understand the data structure and intent of the code overall!

Will Matthews
Will Matthews
8,127 Points

I think you're maybe referring to this chunk?

func addChoiceWith(title: String, story: Story) -> Page {
    let page = Page(story: story)
    return addChoiceWith(title: title, page: page)
}

I think this course could benefit for describing the data model up front a bit more. In the following video, Pasan introduces us to the binary tree structure of our story, which makes this stuff a little more clear. However, he never really goes into why he's modelling the information this way.

As I understand it, Pasan has chosen to introduce use to a new concept here--composition through Method Overloading. In theory, I don't see any reason we couldn't just implement these two steps in separate functions, but I think he's just taken this opportunity to introduce us to this new convention.

What helped me was to come back to this video after going through more of the inner workings. It does make more sense after you see more of "the why" behind these decisions in later videos. I just wish that stuff would've been covered up front.