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

Jon Schenck
Jon Schenck
2,028 Points

Help me understand

What is the correct code and why?

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

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

  func incrementBy(points: Int) {



 }
}

1 Answer

Chase Marchione
Chase Marchione
155,055 Points

Hi Jon,

The challenge wants us to add the points parameter (or, really, the value of that variable) to the current values of width and height.

1) We'll want the points parameter to be of type Double, because width and height are both of type Double. 2) Now we can add 'points' to width and height. Instead of typing out, for example, 'width = width + points', I'll use the assignment operator += and type 'width += points', which means the same thing programmatically.

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

Hope this helps!

Jon Schenck
Jon Schenck
2,028 Points

Got it! Thanks for the help.