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 Unary Operators

Apoorva S
Apoorva S
998 Points

Why do i see an error: "No += candidates produce the expected result type 'Int' " for the following?

lets say levelScore = 1, totalScore = levelScore+=1

Isn't levelScore +=1 Equivalent to levelScrore = levelScore + 1?

hence levelScrore will be equal to 2, and then the same for totalScore

2 Answers

I ran into a similar issue in the past, this stack overflow post helped. (http://stackoverflow.com/a/36338986)

Essentially levelScore += 1 does not return a value. Therefore the assignment to totalScore cannot take place. You have to separate the incrementing of levelScore and the assignment to totalScore.

Apoorva S
Apoorva S
998 Points

Thanks for the explanation.

So the point levelScore +=1 Equivalent to levelScrore = levelScore + 1?

isn't true?

They are equivalent in the sense that they both increment levelScore by 1.