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 2 Creating a Story Helper Methods

Why is there a need for 2 addChoice methods

extension Page { func addChoice(title: String, story: Story) -> Page { let page = Page(story: story)

    return addChoice(title, page: page)
}

func addChoice(title: String, page: Page) -> Page {
    switch (firstChoice, secondChoice) {
    case (.Some, .Some):
        break
    case (.None, .None), (.None, .Some):
        firstChoice = (title, page)
    case (.Some, .None):
        secondChoice = (title, page)
}
    return page
}

}

Michael De La Cruz
Michael De La Cruz
10,800 Points

Because we have one method for switch statements that makes it easier to add a choice to a page and the other is so that we can take a story value.

This is a good question actually. I'm still slightly puzzled. Does anybody have a good explanation in reply to this question? Many thanks.

1 Answer

Nathan Barnes
PLUS
Nathan Barnes
Courses Plus Student 13,996 Points

He created the second method to make it easier to create a story with a page because if you just has the first method:

addChoice(title: String, page: Page) -> Page

You would have to create story object then associate it with the method, he's just done that with second addChoice method so that you pass in the story you want, it goes and creates the page with the related story. Its just to make it easier to create a story associated with a page

Hopefully I explained that ok.

Nathan Barnes that's very helpful. Thank you very much.

Abdulwahab Alansari
Abdulwahab Alansari
15,151 Points

But we could have combined both implementations inside one method, I still don't get it why two methods

extension Page {
    func addChoice(title: String, story: Story) -> Page{
        let page = Page(story: story)
        switch (firstChoice, secondChoice){
        case (.Some, .Some): break
        case (.None, .None), (.None, .Some):
            firstChoice = (title, page)
        case (.Some, .None):
            secondChoice = (title, page)
        }
        return page
    }
}