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

Wing Sun Cheung
Wing Sun Cheung
4,933 Points

The addChoice function, so confusing

Hi coders,

I am having a tough time wrapping my head around the idea of this function in the extension of Page. The most confusing thing is why do we need to implement this in the first place? and how does this extension function help us? what is the solution this function is helping us to solve? thank you

A extension is like a category in Objective-C, like it's name it extends the functionality of the VC or any object you are extending. By creating a extension on Page like they did in the video now you are making addChoiceWith() method default behavior of the class. It does two things extend functionality and also for organization of code.

extension String{ func printMe(){ print("I have a string called" + "" + self) } }

Now with this extension on String you can now have a method printMe that you can call on any String object.

Joshua Baatz
Joshua Baatz
8,532 Points

I feel the same way. I love Pasan, but he talks so fast sometimes and doesn't stress what the heck we are doing. At the end I watched ten minutes of something for which I have no clue what the purpose is. I really liked extensions when they were first explained in the previous lessons. But I'm growing to hate them as I see them filled with convoluted code I don't understand.

Also, it doesn't help that every couple minutes he deletes the code completely and takes a different path. I suspect that makes it seem more confusing than it is.

Gavin Hobbs
Gavin Hobbs
5,205 Points

I too am having a hard time understanding this video. I wish there were some illustrations to make it easier to wrap my head around. Pasan Premaratne is there a better way this could be explained?

Pasan Premaratne
Pasan Premaratne
Treehouse Teacher

Hey folks! I definitely understand the frustration :( At the time this course was made, I didn't have a full curriculum planned and didn't have any good places to highlight the uses of computed properties and functions in an extension. This entire course is due for replacement/refresh..

The reason we implement this is purely as a "convenience" to make it easier to call a method that has more arguments. This is a common pattern. If we have a function that "gets the job done" but is cumbersome to call from another object, a common way of cleaning it up is to write an additional function that is "cleaner" at the call site and just does the set up work you need to do to call the original function.

Does this make sense?

Em Har
Em Har
9,775 Points

I spent an hour trying to understand, Wing Sun Cheung did you finally get it? Care to explain it to me?

1 Answer

Waylan Sands
PLUS
Waylan Sands
Courses Plus Student 5,824 Points

Pasan Premaratne The thing I'm confused about is the way the app will work. Is It like an actual choose your own adventure book where the "choices" are set for each page to guide your way through a preempted narrative?

I feel like we are creating an app where the choices will be rendered at random. I need to keep on watching/creating this app to find out how what I'm making. Quite annoying.

Pasan Premaratne
Pasan Premaratne
Treehouse Teacher

Hi Waylan,

Unfortunately I don’t work at treehouse anymore and can’t change the content.

Your first statement is the correct one. When the app is run it will have a set story where choices are made on each page, but the code is written in a way to make it as flexible as possible when creating the story if that makes sense