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 the User Interface Programmatically Prepare For Segue

nicholasdevereaux
nicholasdevereaux
16,353 Points

prepareForSegue function

The last line of my code only likes 'pageController.page = Adventure.story(name: String)'

It only auto populates 'story(name: String)' after Adventure. The code in the video auto populates just 'story' after Adventure.

I must have done something wrong in another file. Here is a copy of my prepareForSegue function.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "startAdventure" { if let pageController = segue.destinationViewController as? PageController { pageController.page = Adventure.story(name: String) }

nicholasdevereaux
nicholasdevereaux
16,353 Points

When I leave it as pageController.page = Adventure.story, I get an error saying I cannot assign value of type '(String) -> Page' to type 'Page?'

nicholasdevereaux
nicholasdevereaux
16,353 Points

When I leave it as pageController.page = Adventure.story, I get an error saying I cannot assign value of type '(String) -> Page' to type 'Page?'

4 Answers

I made the same mistake so I checked the download project files to see what went wrong.

In InteractiveStory.swift, I had the code:

struct Adventure { static func story(name: String) -> Page {

but it should have read:

struct Adventure { static var story: Page {

Once I changed it, the error in PageController.swift went away.

can you explain why this needed to be changed?

nicholasdevereaux
nicholasdevereaux
16,353 Points

If I remember correctly, the code snippet provided was the end result of the InteractiveStory.swift file. However, the point I was at in the tutorial, my InteractiveStory.swift file should have read - struct Adventure { static var story: Page {, so your viewController should be pageController.page = Adventure.story.

As I progressed, it changed the InteractiveStory.swift file to read - struct Adventure { static func story(name: String) -> Page { (which is what I had to begin with) which changed viewController to Adventure.story(name: String).

They may have updated the code snippet since then. It's been awhile so I hope this is helpful.

Abdulwahab Alansari
Abdulwahab Alansari
15,151 Points

Well, I had been wondering why the story method in the Adventure structure had "name: String" parameter especially that Pasan didn't use it in the method implementation, then I came across the error nicholasdevereaux encountered and immediately I knew it was the "name: String" parameter in the story method, I removed it and it worked. However, I still don't know why Pasan added it in the first place.

Hope this could help the students coming after me :)