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

Code Challenge: Classes and Their Methods

We need to provide a means to increase the size of our Button. So we are going to add a method named incrementBy which will accept a parameter named points. (Within the method add points to the width and height properties).

class Button {
  var width: Double
  var height: Double

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

    func incrementBy(points: Double) -> Double 
    reutrn (width*) 

}

I am not sure what it should be incremented by and what type the points is?

I have not yet solved this but 'points should be of type double. the way question is presented is pretty lame. The purpose of function/method is to increase size, i think what they are saying is you add the value of points to width and height ( i.e if width was 20 and height was 10 and the value points from incrementBy was 5 then the output for width would be 25 and output for height would be 15)

in the end of video amit showed you can do it without a return e.g. something like

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

however I can't get it to work i am close but not there. I would kill for more clarity in the questions on this course it costs so much time. Hope my thoughts help

3 Answers

I got it working. tree houses parser accepted it, Xcode did too, however when i called function it gave a literal error. hopefully next question gets to the root. the below code is the correct answer to question. plug it into Xcode and type Button.incrementBy( etc.....) and see if you can make sense of error you get ( though that error is not part of question.)

class Button {
  var width: Double
  var height: Double

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

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

Thank you so much for the help and yes it wasn't the clearest question. That's what I was most confused about.

well that really works but i still didn't understand how to add a value of double to var in class