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

becky hayes
becky hayes
2,226 Points

Having trouble writing the method to add points to width and height

The challenge is to create a method called incrementBy which will accept a paremeter named 'points' - which i think i've done correctly but then it says (within the method add points to the width and height properties) i'm stuck as to how to do this within the method

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) {
    height = height * points
    width = width * points
 }

1 Answer

Luke Dawes
Luke Dawes
9,739 Points

Hi Becky,

It looks like the Code Challenge is asking you to add the value of the points argument for incrementBy to the width and height stored properties. In your code above, you have instead multiplied the values of those stored properties by the value of points and then reassigned those values back to the stored properties, which isn't quite right.

This passed for me in the Code Challenge:

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
}

Hope that helps!