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

This is a bit confusing. I'm trying to work off of what the video was talking about, but i'm finding myself confused.

I just need this to be explained to me.

operators2.swift
// Enter your code below

var initialScore = 8
var totalScore = initialScore
totalScore += 8

1 Answer

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

I'm taking for granted here that you're stuck at step one. Ok so let's say the user is playing a game, but they saved and exited. When they come back in they pick up with their original score which at this time is 8 points. Now they do something which earns them a new point! So what we want to do now is set both the initial score and the total score to the same number. And we do that like this:

var initialScore = 8
let totalScore = ++initialScore

This takes the inital score and increases it to 9 and then assigns 9 back into total score. Both scores are identical. However, if we try to do it like this:

var initialScore = 8
let totalScore = initialScore++

This will be incorrect. What actually happens in this case is that first the initial score which is 8 is assigned into totalScore and then the initial score is incremented. This means that the total score would just have a value of 8 while initial score would have a value of 9. Hope that makes sense!