We've covered if, else if, else, and switch — all great ways to make decisions in Swift. But there's one more tool in the toolkit that's much more compact than all of them. It's called the ternary conditional operator, and at first glance it looks a little strange. Stick with it though — especially when you get to SwiftUI, you'll be glad you learned it. 🌸
🤔 What Does "Ternary" Even Mean?
You've already used binary operators without realising it. Operators like +, -, == are called binary because they work with two pieces of input:
2 + 5 // two inputs
age >= 18 // two inputs
A ternary operator works with three pieces of input. Swift has exactly one ternary operator — the ternary conditional operator — which is why you'll often just hear it called "the ternary operator".
🧩 How It Works — WTF
Here's the syntax:
condition ? valueIfTrue : valueIfFalse
A handy way to remember the order is WTF:
What is the conditi
Discussion
Jump in and comment!
Get the ball rolling with your comment!