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 Getting Started with iOS Development Swift Recap Part 2

Has anyone successfully completed the code challenge, the one where you have to create a new class named robot?

I`m having some what of a difficult time trying to get past this challenge.

robots.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 func move(direction: String){
        switch direction{
            case "Up" : location.y++
            case "Down" : location.y--
            case "Left" : location.x--
            case "Right" : location.x++
            default : break
        }
  }
}

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Julian.

What you have is pretty much right on the button, except for 2 things.

  1. The ++ and -- has been deprecated in Swift 3 and is no longer accepted in Xcode. You will need to use the += operator instead.
  2. In your override method, the params pass in need to be the same as the original method you are overriding. You are missing the external variable name (or in this case, the absence of _.

Give it another go. I'm sure you'll have it now. :)

Keep Coding! :dizzy:

Thanks heaps Jason.