Learn Courses My Dashboard

How to use Switch or If statements in a SwiftUI View

I need help doing something. Let me give a simple layout of what I’m doing and then I’ll explain what I need.
First off I’m using SwiftUI and Xcode 14.

I have a list with an image and a text description in HStack
In my model I have a string that indicates if my image should be small, medium or large in size, thereby changing the look of my list.

I created 3 different views depending on the size BUT I can’t figure out how to select which view to call.
I can’t use a switch or an if statement as they are not allowed in a view.

Can someone point me in the direction of how to do this? I don’t have a grasp yet on how views and conditional code can work together in SwiftUI, unless I have a button that I can attach code to.

Any help would be appreciated.

Thanks - Ed

Yes they are, you must make sure every “path” of code will return a view

Can you show me the model you have? And I can provide a concrete example of what I mean

When you ask for my model is this what you are looking for:?

struct Restaurant: Identifiable
{
var id = UUID()
var name: String
var image: String
var size: String
}

Yes! It would look something like this

EVERY code path must return a view, here there are 3

  • checking for small
  • checking for medium
  • else is if it isn’t either of those, it defaults to large
  • OR if you didn’t want that and check for large, the else could be an EmptyView() instead
HStack {
   Text(restaurant.name)

   if restaurant.size == "small" {
      Image(restaurant.image) //small image
   } else if restaurant.size == "medium" {
      Image(restaurant.image) // medium image
   } else {
      // must be a large image
      Image(restaurant.image) // large image
   }
}

Hi Mike,

First off, thank you, that is very helpful. I was under the impression this wasn’t possible - which is due to my lack of understanding.

That brings me to my next question.
This occurred because I’m still trying to wrap my head around how views and regular code work together in SwiftUI. Even though Swift took a lot more work to set up the screen layout, everything made sense code wise.

Would you have any resource you could point me to that would help me get a better handle on working with views and code? Sorry if this sounds elementary, but I’ve yet to run into a tutorial (video or written) that addresses this.

I’m actually a decent programmer (really!!!), but I’m old school and this is quite different from my old days. I’m having a lot of fun developing apps for my iPad, but I’m finding that teaching an old dog new tricks is a challenge. The programming world has really changed!!

Regardless if you can point me to anything, thanks for your help. I do see what you’re saying, I could just use more examples to drill it into my brain.

Thanks - Ed

Don’t worry Ed, I started my iOS journey in 2018 at which point I was 65.

You can do it.

I would check out the WWDC videos from Apple.

Basically if you write code in the body of a View it must return a View

That’s why it’s written:

var body: someView {
}

It’s a property that’s called body, and it is of type some View so when you don’t return a View, you get an error

You can write control flows (switches, if statements, etc) to show different views in different scenarios, but they must always return a View

When using a button the action part is different, it’s code / a function to execute when the button is tapped. Because it’s the action, it does NOT have to return a View. The button is the View, the action is actually an input parameter that’s a closure, see here

Button {
   print(“tapped button”)
} label: {
   Text(“Tap”)
}