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!

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 Object-Oriented Swift Classes and Objects Classes and Their Methods

Where did I go wrong in writing a method in a class?

I am doing the exact thing of what the question is making for. why does it still give me an error? How am I interpreting the question wrong? Is it asking for a return type of width and height?

Button.swift
class Button {
  var width: Double
  var height: Double

  init(width:Double, height:Double){
    self.width = width
    self.height = height

    func incrementBy(points: Double) {
        width = width + points
        height = height + points

    }

  }
}

1 Answer

Here's your function:

    func incrementBy(points: Double) {
        width = width + points
        height = height + points

    }

This one I just tested and it passes:

  func incrementBy(points: Double) {
    width += points
    height += points
  }

I'm really puzzled?? width += points is supposed to be shorthand for width = width + points

So then I try a little experiment. I paste just your function in, and it passes. I start over, and this time I paste in all your code, replacing everything that's there, and I get this error message.

swift_lint.swift:12:15: error: cannot assign to 'let' value 'width'
        width = width + points
        ~~~~~ ^
swift_lint.swift:13:16: error: cannot assign to 'let' value 'height'
        height = height + points

But I can't see any difference between your code and theirs, except for spacing!