GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

Switch vs if else in a SwiftUI view... is there a reason to avoid switch?

Forums > SwiftUI

Hey folks.

I seem to remember Paul saying in a video that SwiftUI views perform better in some way when you use if statements to switch between blocks of content instead of switch statements. For example:

VStack {
  if state == .option1 {
    Text()
  } else if state == .option2 {
    Image()
  }
}

...instead of...

VStack {
  switch state {
    case .option1:
      Text()
    case .option2:
      Image()
   }
}

Does anyone else remember this, or did I dream it?

   

It was actually was to use a ternary

struct ContentView: View {
    @State private var selectColor = true

    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundStyle(.tint)
            Text("Hello, world!")
        }
        .padding()
        .background(selectColor ? .green : .red) // <- Like this
    }
}

because the view is already complied, rather this

struct ContentView: View {
    @State private var selectColor = true

    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundStyle(.tint)
            Text("Hello, world!")
        }
        .padding()
        .background {
            if selectColor {
                Color.green
            } else {
                Color.red
            }
        }
    }
}

or this

struct ContentView: View {
    @State private var selectColor = true

    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundStyle(.tint)
            Text("Hello, world!")
        }
        .padding()
        .background {
            switch selectColor {
            case true:
                Color.green
            case false:
                Color.red
            }
        }
    }
}

Where the View would have to redrawn on every change, So it better to use ternary if you can however if the case you can not or get too messy then you can use the others.

   

@NigelGee I do wonder if this is still the same though, up until Swift 5.9 - if and switch were statements only, after that if and switch were also expressions (ternary is an expression) - I would expect if it was used as an expression the compiled code would effectively be the same.

   

Hacking with Swift is sponsored by try! Swift Tokyo.

SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!

Get your ticket here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.