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 trialAaron Zheng
1,517 PointsWhat is +=?
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
}
}
class RoundButton: Button { var cornerRadius: Double = 5.0 }
2 Answers
Maciej Sitko
16,164 Pointsproperty += 1
Is the shorthand notation for
property = property + 1;
In other words, incrementing itself by a number;
jason chan
31,009 Pointsincrement
shorthand is ++
basically plus 1 you'll see it a lot in for loops.
Maciej Sitko
16,164 PointsIs it?
jason chan, Increment shorthand as you present increments value only by one and in the example, this may not be the case. We may want to increment it by arbitrary number.
Plus, to avoid confusion now I think the proper answer would be the specific one for incrementor by one:
++property
As in this sense you'd want to explain incrementing that happens before variable assignments, not after, which on the other hand, looks like this
property++