Post

Replies

Boosts

Views

Activity

Reply to how to select select and use the properties of the instance of the struct selected
Thanks Claude31 Below is the whole code of the two views I have so far I tried your suggestion but i didn't get what i need, so i commented it out when i changed the code i couldn't select any row My objective is to be able to create instances of the DRUG struct in addView that are added to my favorites list in favView, I managed that so far. My next goal is to be able to select a row from my favorite list and be able to use that specific instance of the struct in another place import SwiftUI struct Drug: Identifiable, Codable, Hashable {   var id = UUID()   var name: String   var amount: String   var unitamount : String   var bag: String     var isRead: Bool = false }  class Favoritos: ObservableObject {  @Published var favs = [Drug]() {    didSet {      if let encoded = try? JSONEncoder().encode(favs) {        UserDefaults.standard.set(encoded, forKey: "Favs")      }    }  }    init() {      if let savedItems = UserDefaults.standard.data(forKey: "Favs") {        if let decodedItems = try? JSONDecoder().decode([Drug].self, from: savedItems) {         favs = decodedItems          return        }      }      favs = []    } } struct FavListView: View {   @StateObject var favoritos = Favoritos() //  @State var newdrug = Drug(name: "", amount: "", bag: "")   @State private var showingAddView = false   func removeItems(at offsets: IndexSet) {     favoritos.favs.remove(atOffsets: offsets)   }   @State private var selection: String?   var body: some View {           NavigationView {       VStack{ //        List { //          TextField ("Droga", text: $newdrug.name) //          TextField ("Cantidad", text: $newdrug.amount) //          TextField ("Volumen", text: $newdrug.bag) //        }      //        List(favoritos.favs, id: \.self, selection: $selection) { drug in //             Text("\(drug.name) \(drug.amount) \(drug.unitamount) en \(drug.bag)") //              } //        .toolbar { //          EditButton() //        }                   List         { ForEach(favoritos.favs) { drug in           Text("\(drug.name) \(drug.amount) \(drug.unitamount) en \(drug.bag)")         }.onDelete(perform: removeItems)         }.navigationTitle("Infusiones favoritas")           .navigationBarTitleDisplayMode(.inline)           .accentColor(.black)           .toolbar {             EditButton()           }                   Button ("agregar nuevo favorito") {showingAddView = true }         //        Button ("Add") { //        favoritos.favs.insert(Drug(name: newdrug.name, amount: newdrug.amount, bag: newdrug.bag), at: 0) // // //                }                 }.sheet(isPresented: $showingAddView) {         AddView(favoritos: favoritos)             }     }         }           struct FavListView_Previews: PreviewProvider {       static var previews: some View {         FavListView()       }     }   } import SwiftUI struct AddView: View {   @ObservedObject var favoritos : Favoritos   @State var newdrug = Drug(name: "", amount: "", unitamount: "", bag: "")       var body: some View {     VStack{       List {         TextField ("Droga", text: $newdrug.name)           .disableAutocorrection(/*@START_MENU_TOKEN@*/true/*@END_MENU_TOKEN@*/)         TextField ("Cantidad", text: $newdrug.amount)         TextField ("Unidad", text: $newdrug.unitamount)           .autocapitalization(/*@START_MENU_TOKEN@*/.none/*@END_MENU_TOKEN@*/)         TextField ("Volumen", text: $newdrug.bag)       }                                }                       Button ("Add") {         favoritos.favs.insert(Drug(name: newdrug.name, amount: newdrug.amount, unitamount: newdrug.unitamount, bag: newdrug.bag), at: 0)                                  }             }   } struct AddView_Previews: PreviewProvider {   static var previews: some View {     AddView(favoritos : Favoritos())   } }
Jul ’22
Reply to problem with a series of if statements
else if viewModel.npH > 7.45 && diagnos.resultado3 == "alcalosis metabólica" && (viewModel.expectALCPCO2 + 0.5) < viewModel.npCO2 || viewModel.npCO2 > 55 {         diagnos.resultado4 = "acidosis respiratoria asociada" //        diagnos.resultado2 = ""         diagnos.valorasociado1 = "PCO2 esperada: \(viewModel.expectALCPCO2)"         print ("8.2" , viewModel.npH)       } it printed 8.2 7.099999904632568 even though nph = 7.0999 it executed the closure
May ’22
Reply to problems with consecutive if statements
as you said, on my first screen I enter this three values viewModel.npH = viewModel.npCO2 = viewModel.nHCO3 = when I first press the button It navigates to the second screen and prints 1 2 6 and it stops I back up to my first screen and when I press it again, it correctly checks all the conditional and prints 1 2 6 9
May ’22
Reply to function within a switch case
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'
May ’22
Reply to function within a switch case
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")         }
May ’22