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

Michael Pastran
Michael Pastran
4,727 Points

Button Challenge Class

So im stuck. its asking me to add points to the height and width properties inside of the method.

this is what i was thinking

{ height = height + points width = width + points }

its not asking us to use a return. so i have no clue. i have tried specifying it as self. height and self.width and nothing

thanks for the help

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 )
    {

    }
  }
}

2 Answers

You are close. It should look like this:

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
  }
}
Michael Pastran
Michael Pastran
4,727 Points

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

these are the errors i get when i try that Kyle. that was my initial attempt but it gives me those errors. which i dont understand because we dont have any constants. both stored properties are variables.

Michael Pastran
Michael Pastran
4,727 Points

oh btw this are the instructions (within the method add points to the width and height parameters)

I tried it again by copying and pasting exactly what I posted as my answer and it worked for me. Maybe something is wrong with the challenge.

Michael Pastran
Michael Pastran
4,727 Points

lmfaooooo i got it Kyle. if you look at my code you will see that i didnt close the Init!! -__- i messed up by creating the method pretty much inside of the Initializer. Thanks anyways!!