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 a Simple iPhone App with Swift 2.0 Getting Started with iOS Development Swift Recap Part 2

Anthony Westphal
Anthony Westphal
2,174 Points

My code will not be accepted by your editor

Hi

I have written fully working code in the swim playground however your editor won't accept the implementation of my "Down" implementation. i have verified that it is correct and working in the swift playground but this editor keeps saying it isn't implemented correctly. can you show me what I've either done wrong or help fix the editor.

thanks Anthony

classes.swift
class Point {
    var x: Int
    var y: Int

    init(x: Int, y: Int){
        self.x = x
        self.y = y
    }
}


class Machine {
    var location: Point

    init() {
        self.location = Point(x: 0, y: 0)
    }

    func move(direction: String) {
        print("Do nothing! I\'m a machine!")
    }
}

// Enter your code below

class Robot: Machine {
    var x: Int = 0
    var y: Int = 0

    override init() {
        super.init()
    }

    override func move(direction: String) {
        switch direction    {

        case "Up": ++y

        case "Down": --y

        case "Right": ++x

        case "Left": --x

        default: break

        }
    super.location = Point(x: x, y: y)

    }
}

let robot = Robot()

1 Answer

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

You’re right that your code works in a playground but not in the challenge, and I think the problem is that you’re going about reassigning the location coordinates in a kind of roundabout way, and the challenge must be testing your code in a way that doesn’t allow for this edge case.

Since the Robot class inherits from Machine, an instance of Robot will have a location property with location.x and location.y, and you can just increment those directly rather than creating those other x and y properties.

class Robot: Machine {
    override func move(direction: String) {
        switch direction    {

        case "Up": ++location.y

        case "Down": --location.y

        case "Right": ++location.x

        case "Left": --location.x

        default: break

        }
    }
}


let robot = Robot()   // an instance of Robot
robot.location.x         // 0
robot.location.y        // 0
robot.move("Down")
robot.location.x         // 0
robot.location.y         // -1
Anthony Westphal
Anthony Westphal
2,174 Points

Thanks heaps, that fixed it.