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 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.