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 Enumerations and Optionals Introduction to Enumerations Enums and Objects

Kylie Forbes
Kylie Forbes
3,405 Points

Stuck on enums and optionals code challenge. Please help!

I have gone through the forums, re-watched videos, and cannot figure out how to do this. Please help!

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! Im a machine!")
    }
}

// Enter your code below

class Robot: Machine {

  override init() {
   super.init()
   func move(direction: String)  { 
   switch direction {
    case "Up": self.location.y++
    case "Down": self.location.y--
    case "Left": self.location.x--
    case "Right": self.location.x++
    default: break
   }


   }
  }

}

1 Answer

Keli'i Martin
Keli'i Martin
8,227 Points

You seem to have removed the enum that was given to you in the challenge. The original code looked like this:

class Point {
    var x: Int
    var y: Int

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

enum Direction {
    case Left 
    case Right
    case Up
    case Down
}


class Robot {
    var location: Point

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

    func move(direction: Direction) {
        // Enter your code below
    }
}

Using that enum properly is the key to this challenge. A switch statement will work fine and just remember that to access the value of an enum, you can refer to it as follows: .Left in the case of Left, for example.

Looking at the code you posted, it looks like it comes from a different challenge. Nowhere in the linked challenge does it ask you to modify the implementation of the Robot class except to complete the implementation of the move() function. Is that the case?

Kylie Forbes
Kylie Forbes
3,405 Points

Yes. In my attempt to figure this out, I tried some code from a different challenge (possibly the previous version of this challenge?) that asked for similar movements to be made. I'll start fresh again and keep trying. Thanks!

Keli'i Martin
Keli'i Martin
8,227 Points

No problem. If you get stuck again, I'd be more than happy to assist again. Good luck!