Single line if statement in Swift

BytesGuy picture BytesGuy · Jun 11, 2014 · Viewed 56.7k times · Source

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 

Answer

4aRk Kn1gh7 picture 4aRk Kn1gh7 · Dec 23, 2014

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")
}