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

Eric Mentele
Eric Mentele
7,325 Points

Why won't width allow points to be added?

It keeps saying that I need to add points to width within the method. This is what I have right now:

func incrementBy(points: Double) -> (width: Double, height: Double) {

return (width + points, height + points) }

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: Double, height: Double) {

  return (width + points, height + points) 
  }
}

2 Answers

I tried your code in a Swift Playground and it works, so I think this is just an example of Treehouse preferring a specific answer to pass the quiz.

I was able to complete the quiz by not returning the values, but instead mimicking what we saw in the previous video. Following Amit's example, I simply updated the values by adding points to the existing stored values. So width = width + points.

Both solutions work in the Swift Playground, but their automated grading system prefers this solution.

Hope that helps!

Eric Mentele
Eric Mentele
7,325 Points

Thank you! I was not sure that this question posted. I figured that out after re-watching the video. Realized I was creating new values only relevant to the function rather than changing the class's properties values. This was my answer:

func incrementBy(points: Double) {

    width += points
    height += points

}

Awesome! Glad that worked. Great shorthand with the += operator!