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

Cannot assign value of type '(String)' -> Page' to type 'Page?'

Working on the new InteractiveStory lesson I get this error when trying to build the app to run in the simulator the error is in the ViewController.swift file. I cannot figure out what I have done wrong. Here is the snippet of code where the error is raised:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "startAdventure" {
            if let pageController = segue.destinationViewController as? PageController {
                pageController.page = Adventure.story  // <<<Cannot assign value of type '(String)' -> Page' to type 'Page?'
            }
        }
}

Hi James,

Can you also paste in the code from your Adventure struct? I think the problem is probably there, specifically in your declaration of the story property.

Just hit Add Comment, and then paste your code in between coding tags like this:

```Adventure.swift

[paste your code in here]

```

Thanks Greg for the quick response!

here is the code.

Adventure.swift
struct Adventure {
    static func story(name: String) -> Page {
        let returnTrip = Page(story: .ReturnTrip)
        let touchdown = returnTrip.addChoice("Stop and Investigate", story: .TouchDown)
        let homeward = returnTrip.addChoice("Continue Home to Earth", story: .Homeward)
        let rover = touchdown.addChoice("Explore the Rover", story: .Rover)
        let crate = touchdown.addChoice("Open the Crate", story: .Crate)

        homeward.addChoice("Head back to Mars", page: touchdown)
        let home = homeward.addChoice("Continue Home to Earth", story: .Home)

        let cave = rover.addChoice("Explore the Coordinates", story: .Cave)
        rover.addChoice("Return to Earth", page: home)

        cave.addChoice("Continue towards faint light", story: .Droid)
        cave.addChoice("Refill the ship and explore the rover", page: rover)

        crate.addChoice("Explore the Rover", page: rover)
        crate.addChoice("Use the key", story: .Monster)

        return returnTrip
    }
}

1 Answer

I see what I did wrong. the it should be

static var story: Page

instead of

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

changed that line and everything works better now!

thanks for pointing me to the right place to look.

Yep bingo. Computed property, not method :).

Glad you figured it out!