How would one convert the following to Swift from Objective-C?
if (myVar) return;
Swift does not use parentheses around the conditional, however the following code gives an error.
if myVar return
The expression represented by the expression pattern is compared with the value of an input expression using the Swift standard library ~= operator. The matches succeeds if the ~= operator returns true . By default, the ~= operator compares two values of the same type using the == operator.
Ternary Operator in Swift A ternary operator evaluates a condition and executes a block of code based on the condition. Its syntax is condition ? expression1 : expression2. Here, the ternary operator evaluates condition and. if condition is true, expression1 is executed.
Well as the other guys also explained that braces are a must in swift. But for simplicity one can always do something like:
let a = -5 // if the condition is true then doThis() gets called else doThat() gets called a >= 0 ? doThis(): doThat() func doThis() { println("Do This") } func doThat() { println("Do That") }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With