Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to set a variable value using an if statement?

Tags:

ios

swift

in kotlin you can do this

val height = if height > 3.0 {
    return "giant"
} else {
    return "regular"
}

that is setting the value of a variable or constant using an if statement

is it possible to do the same thing in swift?

let height = if height > 3.0 {
    return "giant"
} else {
    return "regular"
}

I tried this for swift but i get Expected initial value after '=' error

like image 844
Kennedy Avatar asked Nov 17 '25 09:11

Kennedy


1 Answers

You can use ternary operator in Swift

return height > 3.0 ? "giant" : "regular"

Also in Kotlin you can do

return if(height > 3.0) "giant" else "regular"
like image 180
Sh_Khan Avatar answered Nov 18 '25 23:11

Sh_Khan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!