Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Build an Interactive Story App with Swift Creating the User Interface Programmatically Prepare For Segue

Andrew Lundy
Andrew Lundy
13,269 Points

How does it know to get the text from .returnTrip?

Hello. I'm insanely confused on how exactly

if let page = page { print(page.story.text) }

connects directly to the .returnTrip text string.

I understand the setting up of the PageController. I also I understand (I think) that pageController.page = Adventure.story is setting up a value to be accessed. But what I don't get is the page.story.text. Because, if the page of the PageController is set to Adventure.story, then when you look at the Adventure struct, the only 'story' is the type property of story, which inherits Page.

Is this because Adventure.story has class inheritance of Page? Therefore, when print (page.story.text) is called, it's actually accessing the story constant of Page, and then accessing the 'text' variable in the Story file? But even then, how does it now to print out only the .returnTrip text? Shouldn't it print out all of the text in the 'text' variable?

If you can please help me out with this logic, I really don't want to progress in the course until I understand this.

2 Answers

Joshua Hawthorne
Joshua Hawthorne
18,523 Points

text is a computed property from the enum Story. When you type page.story.text you are first getting a reference to the stored property page, then a reference to enum story that is a stored property in the page instance, and finally to the computed property text that is part of the Story enum extension.

When the segue is triggered, the page is initialized with the enum member .returnTrip. Looking at the computed property text we see that the .returnTrip case returns "On your surface trip blah blah blah..."

How do I know its initialized with .returnTrip? This bit of code right here:

static var story: Page {
        let returnTrip = Page(story: .returnTrip)
        //Code that adds choices 
       return returnTrip 
}
Andrew Lundy
Andrew Lundy
13,269 Points

Awesome, thank you so much!

You don't have to answer this, BUT, why do we not set a getter and setter in this computed property? Isn't that a must have for computed properties?

Thanks again!

Joshua Hawthorne
Joshua Hawthorne
18,523 Points

No problem. You don't need to always have both a getter and setter, you only need to have a getter. If you don't have a setter, the computed property you are getting is sort of like a 'let' (a constant). You'll get more familiar with it as you write more code.

It doesn't really make sense in this case to change the text of the story, so we've only implemented a getter here.