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 Swift 2.0 Protocols Creating Flexible Objects Using Protocols Protocol Oriented Programming Part 2

Kevin Gutowski
Kevin Gutowski
4,082 Points

Pasting in code leads to errors (Might be Swift 3 related?)

When I paste in the example code it looks like some of it errors (I'm on Xcode build 8.0 [8S128d]). For the most part I was able to fix them except this last part:

func move(direction: Direction, distance: Int) {
        switch direction {
        case .Up: position.y += 1
        case .Down: position.y -= 1
        case .Left: position.x -= 1
        case .Right: position.x += 1
        }
 }

I was able to fix the errors with position.y and position.x by changing the struct Point to have variables rather than constants.

struct Point {
    var x: Int
    var y: Int
}

However, I'm hesitant to do so because I thought we wanted to keep these as constants in the first place! Any insight into whats going on would be greatly appreciated.

1 Answer

I do not think this is swift 3 related I just believe the code snippet was supposed to be var but was let. In order to change the position the point has to be mutable hence why you need to change it to var