It’s common to want modifiers that apply only when a certain condition is met, and in SwiftUI the easiest way to do that is with the ternary conditional operator.
As a reminder, to use the ternary operator you write your condition first, then a question mark and what should be used if the condition is true, then a colon followed by what should be used if the condition is false. If you forget this order a lot, remember Scott Michaud’s helpful mnemonic: What do you want to check, True, False, or “WTF” for short.
For example, if you had a property that could be either true or false, you could use that to control the foreground style of a button like this:
struct ContentView: View {
@State private var useRedText = false
var body: some View {
Button("Hello World") {
// flip the Boolean between true and false
useRedText.toggle()
}
.foregroundStyle(useRedText ? .red : .blue)
}
}
So, when useRedText
is true the modifier effectively reads .foregroundStyle(.red)
, and when it’s false the modifier becomes .foregroundStyle(.blue)
. Because SwiftUI watches for changes in our @State
properties and re-invokes our body
property, whenever that property changes the color will immediately update.
You can often use regular if
conditions to return different views based on some state, but this actually creates more work for SwiftUI – rather than seeing one Button
being used with different colors, it now sees two different Button
views, and when we flip the Boolean condition it will destroy one to create the other rather than just recolor what it has.
So, this kind of code might look the same, but it’s actually less efficient:
var body: some View {
if useRedText {
Button("Hello World") {
useRedText.toggle()
}
.foregroundStyle(.red)
} else {
Button("Hello World") {
useRedText.toggle()
}
.foregroundStyle(.blue)
}
}
Sometimes using if
statements are unavoidable, but where possible prefer to use the ternary operator instead.
SPONSORED Transform your career with the iOS Lead Essentials. Unlock over 40 hours of expert training, mentorship, and community support to secure your place among the best devs. Click for early access to this limited offer and a FREE crash course.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.