function within a switch case

I'd like to call a function within a switch case

switch drgpick {
        case "Remifentanilo 5 mg en 250 ml" :
          remi5en250()
        }

but it seems I'm obliged to use a button to call the function like so

switch drgpick {
        case "Remifentanilo 5 mg en 250 ml" :
        Button ("\(drgpick)") {
          remi5en250()
        }

is there a way to call the function without having to use a button?

Answered by Claude31 in 712628022

You can call a func in a modifier: .onAppear, .onChange for instance.

So, if the switch executes some code (but do not return View) Create a func

func callAction(for drgpick: String) {
        switch drgpick {
        case "Remifentanilo 5 mg en 250 ml"    : remi5en250()
        case "Remifentanilo 2.5 mg en 250 ml" :  remi25en250()
        case "Noradrenalina 4 mg en 250 ml"   : nora4en250()
        case "Noradrenalina 8 mg en 250 ml" : nora8en250()
        case "Noradrenalina 20 mg en 250 ml" :  nora20en250()
        case "Dexmedetomidina 200 mcg en 100 ml" :  dex200en100()
        case "Dexmedetomidina 200 mcg en 50 ml" : dex200en50()
        case "Milrinona 20 mg en 250 ml" : milri20en250()
        case "Adrenalina 4 mg en 250 ml" : adre4en250()
        default : break  // Not a View here
        }
}

Then call:

Picker("Drogas", selection: $drgpick, content: {
          ForEach(opcdrg, id: \.self, content: { opcdrg in Text(opcdrg) } )
        }
    )
    .onChange(of: drgpick) {_ in callAction(for: drgpick) }

You probably need a "default" clause to handle the switch on string literal.

import Foundation

let text = "Remifentanilo 5 mg en 250 ml"

func remi5en250() {
  print("something")
}

switch text {
  case "Remifentanilo 5 mg en 250 ml":
    remi5en250()  // printed "something"
  default:
    break
}

I already have a default clause but if i dont call the function from within a button it doesnt compile

Picker("Drogas", selection: $drgpick, content: {
          ForEach(opcdrg, id: \.self, content: { opcdrg in
            Text(opcdrg)
          })
        })
        switch drgpick {
        case "Remifentanilo 5 mg en 250 ml" :
        Button ("\(drgpick)") {
          remi5en250()
        }
        case "Remifentanilo 2.5 mg en 250 ml" :
        Button ("\(drgpick)") {
          remi25en250()
        }
        case "Noradrenalina 4 mg en 250 ml" :
        Button ("\(drgpick)") {
          nora4en250()
        }
        case "Noradrenalina 8 mg en 250 ml" :
        Button ("\(drgpick)") {
          nora8en250()
        }
        case "Noradrenalina 20 mg en 250 ml" :
        Button ("\(drgpick)") {
          nora20en250()
        }
        case "Dexmedetomidina 200 mcg en 100 ml" :
        Button ("\(drgpick)") {
          dex200en100()
        }
        case "Dexmedetomidina 200 mcg en 50 ml" :
        Button ("\(drgpick)") {
          dex200en50()
        }
        case "Milrinona 20 mg en 250 ml" :
        Button ("\(drgpick)") {
          milri20en250()
        }
        case "Adrenalina 4 mg en 250 ml" :
        Button ("\(drgpick)") {
          adre4en250()
        }
        default : Text ("nada")
        }

         

What error do you get ? Where ?

I have tested code with switch:

struct firstView: View {
    var body: some View {
        Text("Welcome to firstView")
    }
}

struct ContentView: View {
    @State var page = "first"
    
    var body: some View {
        switch page {
        case "first": firstView()
        default: Text("Default")
        }
    }
}

It compiles and run.

So the error is probably elsewhere.

Please provide complete code.

How do i submit the complete code? It tells me that i went over the maximun numbers of characters

switch drgpick {
        case "Remifentanilo 5 mg en 250 ml" :
        //Button ("\(drgpick)") {
          remi5en250()
        //}
        case "Remifentanilo 2.5 mg en 250 ml" :
        Button ("\(drgpick)") {
          remi25en250()
        }
        case "Noradrenalina 4 mg en 250 ml" :
        Button ("\(drgpick)") {
          nora4en250()
        }
        case "Noradrenalina 8 mg en 250 ml" :

this are the errors i get Type '()' cannot conform to 'View' Only concrete types such as structs, enums and classes can conform to protocols Requirement from conditional conformance of '_ConditionalContent<(), Button>' to 'View'

I suspect you defined remi5en250() as a func, not a struct returning a View. That would explain the error.

To work without Button, you need to have it return a View, as:

struct remi5en250: View {
    var body: some View {
        Text("remi5en250")
    }
}

That's the architecture of SwiftUI. But seeing more code, we may find other solutions.

Accepted Answer

You can call a func in a modifier: .onAppear, .onChange for instance.

So, if the switch executes some code (but do not return View) Create a func

func callAction(for drgpick: String) {
        switch drgpick {
        case "Remifentanilo 5 mg en 250 ml"    : remi5en250()
        case "Remifentanilo 2.5 mg en 250 ml" :  remi25en250()
        case "Noradrenalina 4 mg en 250 ml"   : nora4en250()
        case "Noradrenalina 8 mg en 250 ml" : nora8en250()
        case "Noradrenalina 20 mg en 250 ml" :  nora20en250()
        case "Dexmedetomidina 200 mcg en 100 ml" :  dex200en100()
        case "Dexmedetomidina 200 mcg en 50 ml" : dex200en50()
        case "Milrinona 20 mg en 250 ml" : milri20en250()
        case "Adrenalina 4 mg en 250 ml" : adre4en250()
        default : break  // Not a View here
        }
}

Then call:

Picker("Drogas", selection: $drgpick, content: {
          ForEach(opcdrg, id: \.self, content: { opcdrg in Text(opcdrg) } )
        }
    )
    .onChange(of: drgpick) {_ in callAction(for: drgpick) }
function within a switch case
 
 
Q