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 Swift 2.0 Basics Swift Operators Working With Operators: Part 2

Suban Purushothmakumar
Suban Purushothmakumar
3,306 Points

Shouldn't the correct answer to task 1 be let totalScore = initialScore++

You stated that you want the variable and the constant to have the same value, but adding the incremental operator at the start would give initialscore = 8 and totalscore=9

operators2.swift
// Enter your code below

var initialScore = 8

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

No, it wouldn't. If you did it with the ++ operator after, it would work like this. First, you would put initialScore into totalScore. Which would put 8 in there. And then you would increment initialScore. Which means that totalScore would end up with an 8, but initialScore would end up with a 9.

The ++ before the initialScore will first increment it to 9. Then assign the 9 into totalScore. In this instance, the initialScore and totalScore will now hold the same value. Hope that helps! :smiley:

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Jennifer is absolutely correct (and she usually is :smiley:), but just to add a note. You shouldn't get too used to these as they are deprecated in Xcode 7.3.

You can read about it in this Stack Overflow Thread or one of the statements from Chris Lattner (aka the developer of the Swift Language).

:dizzy:

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Good point, Jason Anders! However, as an aside to your side note, I'd like to add that while these increment and decrement operators are being deprecated in Swift, they are still around in other languages. For example: Java, C++/C#, and PHP. So knowing why it works this way may help if only to solidify your understanding when you get to another language that does use them :smiley: