Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Protocols in Swift Creating Flexible Objects Using Protocols Protocol Oriented Programming Part 2

JaWaune Long
JaWaune Long
2,511 Points

Where are we using distance?

We have this method in the Moveable protocol
func move(_ direction: Direction, by distance: Int)

But we didn't use the distance argument for anything in the method, what's the point of this?

2 Answers

Otto linden
Otto linden
5,857 Points

Hello! In the move function and in the direction switch we case

position = Point(x: position.x, y: position.y + 1)

so first the direction which is x or y and then distance and that is in this case position.x and position.y + 1, so the x position stays the same and the y position + 1! Hope this helps! 🙂

It looks like the instructor meant to implement the function like this:

func move(_ direction: Direction, by distance: Int) {
     switch direction {
     case.up: position = Point(x: position.x, y: position.y + distance)
     case.down: position = Point(x: position.x, y: position.y - distance)
     case.right: position = Point(x: position.x + distance, y: position.y)
     case.left: position = Point(x: position.x - distance, y: position.y)
     }
}

My guess is that he just forgot to make use of that distance argument in the midst of writing out the implementation, instead using a fixed distance (of 1) for each direction.