Jeff Morhous
Jeff Morhous Jeff is a Software Engineer and Senior Computer Science Student. He enjoys making new things, fixing old things, and learning from everything.

How To Use Swift’s Ternary Operator

How To Use Swift’s Ternary Operator

Ternary operators are beyond cool. They’re an easy way to do some if-else logic all on one line. Beyond that, they’re still easy to read and understand, so you don’t have to sacrifice readability for the ‘cool factor’ of using a language’s advanced features. In fact, it’s a great way to keep methods shorter and draw attention to other logic that may be more meaningful to your program.

Start With An If-Else Statement

Take the below code for example. You compare two variables, and print something based on the result.

1
2
3
4
5
if variable1 == variable2 {
    print("Equality! 🥳")
} else {
    print("NO EQUALITY 🤬")
}

Convert It To One Line

The ternary operator consists of 3 parts.

  1. A conditional check
  2. The result of the operation if the conditional is true
  3. The result of the operation if the conditional is false

For our above if-else statement, we could write it like this 👇

1
print(variable1 == variable2 ? "Equality! 🥳" : "NO EQUALITY 🤬")

Assigning A Value

Ternary operations in Swift aren’t limited to just print statements. In fact, the ternary operator returns a value which can be used to set a variable.

You could take the below check for the age to drink alcohol

1
2
3
4
5
6
7
8
9
var result = “”

if(age < 21){  
  result = "Too young to drink 🍺"  
} else {  
  result = "Old enough to drink 🍻"  
}

print(result)

This is a lot of code to just assign a string based on a number value. We can use the ternary operator to do this in just 2 lines — and you can see the example below 👇

1
2
let result = age > 21 ? "Too young to drink 🍺"" : "Old enough to drink 🍻"
print(result)

If you found this helpful, you might enjoy checking out my book on learning to program with Swift!