iOS Build a Simple iPhone App with Swift Getting Started with iOS Development Swift Recap Part 2

Erin Anthony
Erin Anthony
iOS Development with Swift Techdegree Student 1,732 Points

unresolved identifier error

Hi, I'm totally stuck at this point on the Swift Recap Part 2 challenge. No matter how I try to modify my code I still get some type of error message. The message I seem to get the most is "use of unresolved identifier 'y', use of unresolved identifier'x'." I understand what the error means (Swift can't figure out what 'x' and 'y' are) but I can't figure out how to fix it. I've tried Google but the info I've found is too broad for my specific problem. I've tried multiple variations of adding an override init to pass through x and y, and made multiple attempts to pass them directly to the overridden function, but I couldn't get it to work either way. Thanks!

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

// Enter your code below
class Robot: Machine {

  override func move(direction: String) {
    switch direction {
      case "Up": y += 1
      case "Down": y -= 1
      case "Left": x -= 1
      case "Right": x += 1
      default: break
    }
  }
}

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Staff

Hi there, Erin Anthony ! First, you need to tell Swift that you're going to omit the argument label by giving it an underscore _, which somehow got removed from the beginning code.

I would expect to see:

  func move(_ direction: String)

and

 override func move(_ direction: String)

Once that is corrected, you will still have the problem with the unresolved x and y. This is because x and y are properties on a Point that is assigned to self.location. So where you have:

case "Up": y += 1

I would expect to see:

case "Up": location.y += 1

I think you can get it with these hints, but let me know if you're still stuck! :sparkles: