0
Guys pleeeaaaase explain me what do a quetion mark and a colon mean in swift?
2 Réponses
+ 4
When you see an expression like this:
{conditional statement} ? {one thing} : {second thing}
it is the same as:
if ({conditional statement}) {one thing}
else {second thing}
the question mark and colon is just a shorter way of writing the if...else statement.
0
it's called a ternary if statement. it's a shorthand way of writing an if else statement.
Instead of writing this:
var y: Bool
car x = true
if (x == true) {
y = true
} else {
y = false
}
you can write:
y = (x ? true : false)
You would use it when you need to set a variable to a or b, or return or print a or b