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

help on my code

I'm trying to work on my code but its failing inherit the machine function

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

  init(x: Int, y: Int) {
    s
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 am a machine!")
    }
}

// Enter your code below
enum Direction {
    case left
    case right
    case up
    case down
}


class Robot: Machine {
    var relocation: Point

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

   override func move(_ direction: Direction) {

        switch direction {
        case .up: location.y += 1
        case .down: location.y -= 1
        case .right: location.x += 1
        case .left: location.x -= 1
        default: return print(" no input")
        }
    }
}

1 Answer

Hi there,

Your code seems to have got a bit muddled as there's two declarations of the Point class - best tidy up the code that the challenge starts with as you won't need to amend any of that.

Next, the directions are given to the move method. They are of datatype String - you can't amend that as the tests will test for sending a String into move and seeing the Robot "move".

So, delete your enum; it isn't needed. Have the move function take a String parameter and switch on that parameter. The moving part you've done fine but you just need to tweak your statement along the lines of:

    switch direction {
      case "Up": location.y += 1
.
.

I don't think you need to override init and the variable, relocation isn't required either, I don't think. Have a looko at your default case too - maybe just break out rather than returning anything?

I hope that helps,

Steve.