Gean Ginanjar Gean Ginanjar - 7 months ago 13
Swift Question

iOS Swift assign variable value with conditional

back in objective-c i can assign value of a variable with a result of conditional

EX :

int b = 10
int c = 11
int a = b!=c ? 90 : 100;


But now on swift i cannot do that no more.

var b = 10
var c = 11
var a = b > c ? 90 : 100


the code above always highlight the
var a
line as error with desc :
Expected expression
with suggestion of adding
;


is there a way to assign variable with conditional with swift?

thanks!

Answer

I got your problem. you are trying to write code like this

  var b = 10
  var c = 11
  var value = b >c ? 90 : 100

which is wrong.

instead write code with extra space after >

var a = b > c ? 90 : 100
Comments