Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Switch statement for category selection and output

So I have this part in my code, where I will have to create like 14 if/else conditionals, so I wanted to see if someone knows a better way for me to handle a Image render with multiple conditions.

So this is what I have:

if placeC.places[0].category == "Route" {
    Image("map-pin")
        .renderingMode(.template)
        .resizable()
        .frame(width: 35, height: 35)
        .foregroundColor(placeC.places[0].show ? .red : Color.init(UIColor(hexString: "#6A798E")))
        .onTapGesture {
            if !placeC.places[0].show {
                tapOnMapAnnotation(
                    place: placeC.places[0]
                )
            }
        }
        .overlay(
            Awesome.Image(icon: Awesome.Solid.route)
                .size(12)
                .foregroundColor(.white)
                .padding(.bottom, 15)
        )
} else if placeC.places[0].category == "Scenic Area" {
    Image("map-pin")
        .renderingMode(.template)
        .resizable()
        .frame(width: 35, height: 35)
        .foregroundColor(placeC.places[0].show ? .red : Color.init(UIColor(hexString: "#006994")))
        .onTapGesture {
            if !placeC.places[0].show {
                tapOnMapAnnotation(
                    place: placeC.places[0]
                )
            }
        }
        .overlay(
            Awesome.Image(icon: Awesome.Solid.binoculars)
                .size(12)
                .foregroundColor(.white)
                .padding(.bottom, 15)
        )
     etc...

Everything is the same, except the category and the hexString: "----" – How would I be able to utilize a function or possible a switch statement on this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can abstract it into

func hexStringForCategory(_ category: String) -> String {
  switch category {
    case "Route":
      return "#6A798E"
    case "Scenic Area":
      return "_DIFFERENT_COLOR_"
    //etc..
    default:
      return "#FFFFFF"
  }
}

@ViewBuilder func viewForPlace(_ place: Place) -> some View {
  Image("map-pin")
        .renderingMode(.template)
        .resizable()
        .frame(width: 35, height: 35)
        .foregroundColor(place.show ? .red : Color.init(UIColor(hexString: hexStringForCategory(place.category))))
        .onTapGesture {
            if !place.show {
                tapOnMapAnnotation(
                    place: place
                )
            }
        }
        .overlay(
            Awesome.Image(icon: Awesome.Solid.route)
                .size(12)
                .foregroundColor(.white)
                .padding(.bottom, 15)
        )
}

Then, you could just call:

viewForPlace(placeC.places[0])

If you’re only ever going to do this for placeC.places[0], you don’t even need the second function — you can just use the first.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading