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 trialNicol Luis Yumang
12,598 PointsWhy 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
}
}
Chris Smith
6,791 PointsThis 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
Courses Plus Student 13,996 PointsHe 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.
Chris Smith
6,791 PointsNathan Barnes that's very helpful. Thank you very much.
Abdulwahab Alansari
15,151 PointsBut 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
}
}
Michael De La Cruz
10,800 PointsMichael De La Cruz
10,800 PointsBecause 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.