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

Kristen Turner
Kristen Turner
925 Points

Comparison Operation passes code challenge but receives error in Swift

For the last task of the code challenge, the following passed: let isWinner = totalScore != 10

However, Swift gives me this error: Binary operator '!=" cannot be applied to operands of type '()' and 'Int'

Is this telling me that the NOT EQUALS operator i.e. != cannot be applied to Integers? How do I properly write 'let isWinner = totalScore != 10' for Swift?

2 Answers

David Lin
David Lin
35,864 Points

Yeah, you can't do this:

let totalScore = initialScore += 1

You can only assign if you're doing a uniary operation (e.g. let totalSocore = ++initialScore), but as you pointed out, those were defuncted in Swift 3.

So, to achieve what you want to do, you'll have to do this instead:

var initialScore = 8
initialScore += 1
let totalScore = initialScore 
let isWinner = totalScore != 10
David Lin
David Lin
35,864 Points

You're welcome, Kristen!

David Lin
David Lin
35,864 Points

Hmm, this compiles and runs perfectly fine for me without error.

let totalScore = 10
let isWinner = totalScore != 10

Maybe double check if your totalScore value is an Int?

Kristen Turner
Kristen Turner
925 Points

For this code challenge, the entire code looks like this:

var initialScore = 8
let totalScore = initialScore += 1
let isWinner = totalScore != 10

(Technically, the code challenge expects the second line to be let totalScore = ++initialScore but that's not supported in Swift 3.)

That gives the error: Binary operator '!=" cannot be applied to operands of type '()' and 'Int'

I tried putting

let totalScore: Int = initialScore += 1

but that results in a new error of " '+=' produces '()', not the expected contextual result type 'Int'