Post

Replies

Boosts

Views

Activity

how to select select and use the properties of the instance of the struct selected
So far, and with help from this forum, I have an app that that lets the user create a favorites list of Drugs. Drug is a struct. I can add and delete instances of the struct. But my question is, how do i select one favorite, and how can i acces the properties of the struct selected import SwiftUI struct Drug: Identifiable, Codable {   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)   }   var body: some View {           NavigationView {       VStack{ //        List { //          TextField ("Droga", text: $newdrug.name) //          TextField ("Cantidad", text: $newdrug.amount) //          TextField ("Volumen", text: $newdrug.bag) //        }                   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)                   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())   } }
3
0
533
Jun ’22
creating a list from an array of structs
I'm trying to populate an array of structs from textfield entries but when i add an entry it replaces all other entries with the last one. import SwiftUI struct Drug: Hashable, Codable, Identifiable {  var id = UUID()  var name: String  var amount: String  var bag: String     var isRead: Bool = false } extension Drug {  static let samples = [   Drug(name: "", amount: "", bag: ""), ] }  class BooksViewModel: ObservableObject {  @Published var drugs: [Drug] = Drug.samples  @Published var favs : [Drug] = []     } struct BooksListView: View {   @StateObject var viewModel = BooksViewModel()   @State var newdrug : Drug = Drug(name: "", amount: "", bag: "")       var body: some View {     VStack{       List {         TextField ("Droga", text: $newdrug.name)         TextField ("Cantidad", text: $newdrug.amount)         TextField ("Volumen", text: $newdrug.bag)       }               List (viewModel.favs) { drug in         Text ("\(drug.name) \(drug.amount) in \(drug.bag)")       }               Button ("Add") {         viewModel.favs.insert(newdrug, at: 0)         print(viewModel.favs)       }             }   }               struct BooksListView_Previews: PreviewProvider {     static var previews: some View {       BooksListView()     }   } }
2
0
850
Jun ’22
problem with a series of if statements
My app has a series of if statements. some of them a executed even when the condition is not met.  if viewModel.npH > 7.45 {         diagnos.resultado1 = "alcalemia"         print ("1")         }       if viewModel.npCO2 < 35 {         diagnos.resultado2 = "alcalosis respiratoria"         print ("2")         }       if viewModel.nHCO3 > 26 {         diagnos.resultado3 = "alcalosis metabólica"         print ("3")       }               if viewModel.npH < 7.35 {         diagnos.resultado1 = "acidemia"         print ("4")       }       if viewModel.npCO2 > 45 {         diagnos.resultado2 = "acidosis respiratoria"         print ("5")       }       if viewModel.nHCO3 < 22 {         diagnos.resultado3 = "acidosis metabólica"         print ("6")       } For instance when i run it with viewModel.nph = 7.4, viewModel.npCO2 = 40 and viewModel.nHCO3 = 23.9 It prints "4", "5", "6" even though none of the conditions are met. I'm obviously doing something wrong but I can't understand what is it I' m doing wrong
4
0
737
May ’22
form top row
how do i get rid of this grey top row?  var body: some View {           NavigationView {     Form {       HStack {         Form {                      TextField("pH", text: $viewModel.pH)             .modifier(TextFieldClearButton(text: $viewModel.pH))           TextField("pCO2", text: $viewModel.pCO2)             .modifier(TextFieldClearButton(text: $viewModel.pCO2))                       TextField("HCO3", text: $viewModel.HCO3)             .modifier(TextFieldClearButton(text: $viewModel.HCO3))           TextField("Glu", text: $viewModel.Glu)             .modifier(TextFieldClearButton(text: $viewModel.Glu))                       Spacer()                             }.frame(width: /*@START_MENU_TOKEN@*/150.0/*@END_MENU_TOKEN@*/, height: 200).lineSpacing(/*@START_MENU_TOKEN@*/5.0/*@END_MENU_TOKEN@*/)                 Form {                       TextField("Na", text: $viewModel.Na)              .modifier(TextFieldClearButton(text: $viewModel.Na))            TextField("K", text: $viewModel.K)              .modifier(TextFieldClearButton(text: $viewModel.K))            TextField("Cl", text: $viewModel.Cl)              .modifier(TextFieldClearButton(text: $viewModel.Cl))            TextField("Alb", text: $viewModel.Alb)              .modifier(TextFieldClearButton(text: $viewModel.Alb))                     }.frame(width: /*@START_MENU_TOKEN@*/150.0/*@END_MENU_TOKEN@*/, height: /*@START_MENU_TOKEN@*/200.0/*@END_MENU_TOKEN@*/)          .lineSpacing(/*@START_MENU_TOKEN@*/5.0/*@END_MENU_TOKEN@*/)
1
0
586
May ’22
problems with consecutive if statements
I made a function that checks over several conditions tu return a string. the function is called when a navigation link is pressed the issue I'm having is that it breaks after the third if statement and only keeps checking after pressing the navigation link for the second time. func diag() {       viewModel.npH = Double(Float(viewModel.pH) ?? 0)       viewModel.npCO2 = Double(Float(viewModel.pCO2) ?? 0)       viewModel.nHCO3 = Double(Float(viewModel.HCO3) ?? 0)       if viewModel.npH > 7.45 {         diagnos.resultado1 = "alcalemia"         print ("1")         }       if viewModel.npCO2 < 40 {         diagnos.resultado2 = "alcalosis respiratoria"         print ("2")         }       if viewModel.nHCO3 > 24 {         diagnos.resultado3 = "alcalosis metabólica"         print ("3")       }               if viewModel.npH < 7.35 {         diagnos.resultado1 = "acidemia"         print ("4")       }       if viewModel.npCO2 > 40 {         diagnos.resultado2 = "acidosis respiratoria"         print ("5")       }       if viewModel.nHCO3 < 24 {         diagnos.resultado3 = "acidosis metabólica"         print ("6")       }       if diagnos.resultado1 == "acidemia" && diagnos.resultado3 == "acidosis metabólica" && (viewModel.expectPCO2aci - 2)...(viewModel.expectPCO2aci + 2) ~= viewModel.npCO2 {         diagnos.resultado4 = "acidosis metabólica compensada"         diagnos.resultado2 = ""         print ("7")         }       if diagnos.resultado1 == "alcalemia" && diagnos.resultado3 == "alcalosis metabólica" && (viewModel.expectALCPCO2 - 0.5)...(viewModel.expectALCPCO2 + 0.5) ~= viewModel.npCO2 {         diagnos.resultado4 = "alcalosis metabólica compensada"         diagnos.resultado3 = ""         print ("8")       }       if diagnos.resultado1 == "alcalemia" && diagnos.resultado2 == "alcalosis respiratoria"         && viewModel.agucro == 1         && (viewModel.expectHCO3alkA - 0.5)...(viewModel.expectHCO3alkA + 0.5) ~= viewModel.nHCO3 {         diagnos.resultado4 = "alcalosis respiratoria compensada"         diagnos.resultado3 = ""         print ("9")       }             }    }
4
0
753
May ’22
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?
7
0
1.6k
May ’22
assign value to a bool if contains statement is true
I'd like to make a boolean var true if a string var contains a comma. but keep getting an error import SwiftUI struct ContentView: View {   @State var dos = ""   @State var coma : Bool = false       var body: some View {     Form {     TextField("dosis", text: $dos)               if dos.contains(",") == true{       coma == true     }     } } } struct ContentView_Previews: PreviewProvider {   static var previews: some View {     ContentView()   } }
1
0
402
Apr ’22