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 trialJack Colosky
Courses Plus Student 2,263 PointsI think Im getting an error because of my Incrementing, pleas help!
I tried this code in Xcode and the "initialScore" and "totalScore" values were the same, just like the directions said. Is it some sort of silly syntax? Please help!
// Enter your code below
var initialScore = 8
initialScore = ++initialScore
let totalScore = initialScore
1 Answer
Steven Deutsch
21,046 PointsHey Jack Colosky,
You don't have to assign the incremented value back to the variable storing that value. The ++ operator will update the value stored in that variable.
If you use the prefix ++ operator, the value of initialScore (which starts as 8), will be incremented to a value of 9. This new value will then be assigned to the constant totalScore.
// Enter your code below
var initialScore = 8
let totalScore = ++initialScore
It's important to note that the prefix ++ and postfix ++ operator, as well as its counterpart -- operator, will not be supported in Swift 3.0. The correct way to do this will be:
var initialScore = 8
initialScore += 1
let totalScore = initialScore
Yes this does require an extra line of code, however, the Swift community believes this approach to be clearer.
Good Luck