+1

Swift ternary expression

I’m try to write a ternary expression for my code, but I keep getting an error: “result values in '? :' expression have mismatching types '()' and 'Int' c%2 == 0 ? d = 0: d = 1” My code has the following: let c = 7 var d = 0 c%2 == 0 ? d = 0 : d = 1 It should just set d to 0 or 1 depending on whether c is even or odd. I don’t understand what is wrong from the error message.

7/19/2019 6:40:01 PM

Benjamin

1 Answer

New Answer

+5

Benjamin your expression should be c%2 == 0 ? (d=0) : (d=1) or d = c%2==0 ? 0 : 1 with c%2==0 ? d = 0 : d = 1, it's probably treating d and 0 or 1 as different result types. And you are not capturing the result.