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

Asad Chishty
Asad Chishty
422 Points

I've added 1 point to the initialScore and assigned it to a constant called totalScore but it isn't correct

Shouldn't this be correct?

If I'm adding +1 to the initialScore and assigning the result of that operation to the totalScore, wouldn't it simply be var totalScore = initialScore + 1 ?

I tried var totalScore = initialScore so that the values would be the same but that didn't work either.

operators2.swift
// Enter your code below

var initialScore = 8
initialScore = initialScore + 1

var totalScore = initialScore + 1

2 Answers

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

Hi there! There are a few things going on here, but most of them are to do with following the instructions of the challenge. First, the challenge explicitly states that totalScore should be declared as a constant. Secondly, you're supposed to be using the increment operator. Third, when this code ends initialScore and totalScore should have the same value. Your code will have initialScore set to 9 and totalScore set to 10.

Take a look:

// Enter your code below
var initialScore = 8
let totalScore = ++initialScore 

This will increment initialScore (which changes the value) and then assign it back into totalScore. So initialScore will be 9 and then that 9 will be assigned to total score. It's important to note that you can not use the post-increment operator here. You cannot do let totalScore = initialScore++. That would assign the value of 8 into totalScore and then increment the value of initialScore to 9.

It also feels important to point out that these post and pre increment and decrement operators have been deprecated in Swift 3.

Hope this helps! :sparkles:

Asad Chishty
Asad Chishty
422 Points

Thanks so much Jennifer!

I still need to get used to the terminology lol And I've noticed I tend to learn better by being able to ask questions and get clarifications. Thanks so much for being patient and explaining it all so well!