Post

Replies

Boosts

Views

Activity

Comma not working
Hello Do you know why the comma in this calculator app isn't working: import SwiftUI import Combine enum Nums: String{ case uno = "1" case due = "2" case tre = "3" case quattro = "4" case cinque = "5" case sei = "6" case sette = "7" case otto = "8" case nove = "9" case zero = "0" case moltiplicazione = "X" case divisione = "/" case somma = "+" case meno = "-" case uguale = "=" case AC = "AC" case piùMeno = "±" case percentuale = "%" case virgola = "." case niente = "" } struct ContentView: View { var numeri: [[Nums]] = [ [Nums.AC, Nums.piùMeno, Nums.percentuale, Nums.divisione], [Nums.sette, Nums.otto, Nums.nove, Nums.moltiplicazione], [Nums.quattro, Nums.cinque, Nums.sei, Nums.meno], [Nums.uno, Nums.due, Nums.tre, Nums.somma], [Nums.zero, Nums.niente, Nums.virgola, Nums.uguale] ] private var gridItemLayout = [GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible())] @State var previousVar: Double = 0 let timer = Timer.publish(every: 0.001, on: .main, in: .common).autoconnect() @State var con: Int = 0 @State var final: Double = 0 @State var operat = Nums(rawValue: "") @State var digits: String = "0" var res: Double { get { //digits.hasSuffix(".") ? Double(digits.dropLast()) ?? 0.0 : Double(digits) ?? 0.0 digits.hasSuffix(".") ? Double(digits) ?? 0.0 : Double(digits) ?? 0.0 } nonmutating set { digits = String(format: "%.10g", newValue) //- May need better formatting } } var body: some View { VStack{ Spacer() HStack{ Spacer() Text("\(digits)") .font(.system(size: 50)) .bold() .padding() } Spacer() LazyVGrid(columns: gridItemLayout, content: { ForEach(0..5){ i in ForEach(0..4){ j in RoundedRectangle(cornerRadius: 35.0) .foregroundColor(.orange) .frame(width: 80, height: 80, alignment: .center) .overlay( Text("\(numeri[i][j].rawValue)") .font(.largeTitle) .foregroundColor(.black) ).onTapGesture { switch numeri[i][j] { case Nums.AC: operat = Nums.AC; res = 0 case Nums.uguale: operat = Nums.uguale; res = final case Nums.somma: operat = Nums.somma; previousVar = res; res = 0; con = 0 case Nums.meno: operat = Nums.meno; previousVar = res; res = 0 con = 0 case Nums.divisione: operat = Nums.divisione; previousVar = res; res = 0; con = 0 case Nums.moltiplicazione: operat = Nums.moltiplicazione; previousVar = res; res = 0; con = 0 case Nums.percentuale: operat = Nums.percentuale; res = res / 100 case Nums.piùMeno: operat = Nums.piùMeno; if digits.hasPrefix("-") { digits = String(digits.dropFirst()) } else { digits = "-" + digits } con = 0 case Nums.virgola: operat = Nums.virgola; if !digits.contains(".") { digits += "." } default: if digits == "0" { digits = numeri[i][j].rawValue } else { digits += numeri[i][j].rawValue } con += 1 } } } } }).onReceive(timer) { _ in if con != 0 { if operat == Nums.divisione{ final = previousVar / res } else if operat == Nums.meno{ final = previousVar - res } else if operat == Nums.moltiplicazione{ final = previousVar * res } else if operat == Nums.somma{ final = previousVar + res } } } }.padding(2) } } Maybe at line 51 and 52 or 121 (If you can explain my mistakes all around the code I would be grateful) Thank you
10
0
867
Mar ’21
ForEach and HStack
Hello Is there any way to put the 2 Texts in an HStack without putting the 2 ForEachs together? Code: ForEach(model.data, id: \.objectID){ obj in Text(model.getValue(obj: obj)) }.onDelete(perform: model.deleteData) ForEach(date.data, id: \.objectID){ obj1 in Text("\(date.getValue(obj: obj1), formatter: dateFormatter)") }.onDelete(perform: date.deleteData) Thank you
8
0
1.7k
Mar ’21
.trim() Shape SwiftUI
Hello I have a Raindrop shape: struct Raindrop: Shape { func path(in rect: CGRect) - Path { Path { path in path.move(to: CGPoint(x: rect.size.width / 2, y: 0)) path.addQuadCurve(to: CGPoint(x: rect.size.width / 2, y: rect.size.height), control: CGPoint(x: rect.size.width, y: rect.size.height)) path.addQuadCurve(to: CGPoint(x: rect.size.width / 2, y: 0), control: CGPoint(x: 0, y: rect.size.height)) } } } When I trim it in the ContentView, it trims from right to left, is there a way to trim it from top to bottom? Raindrop() .trim(from: 0.9, to: 1) .scaledToFit() Thank you
7
0
2.3k
Apr ’21
Index Out of Range
Hello How do I solve the Index out of range at line 81 I tried many ways, but none of them worked // // ContentView.swift // Fede // // Created by Jad Taljabini on 14/03/21. // import SwiftUI struct ContentView: View { @State private var media: String = "" @State private var voto: String = "" @State private var voti: [Float] = [] @State private var nVoti: Int = 0 @State private var test: String = "" @State private var voti2: [Float] = [] let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect() var body: some View { NavigationView { Form { Section{ TextField("A che media vuoi arrivare", text: $media) } Section{ TextField("Quanti voti prenderai", text: $test) } Section{ HStack { TextField("Tuoi voti", text: $voto) Spacer() Button(action: { voti.append(Float(voto) ?? 0) voto = "" nVoti = nVoti + 1 }, label: { Text("Add") }) } } Section{ ForEach(voti, id: \.self){ item in Text("\(item, specifier: "%.1f")") }.onDelete(perform: removeRows) } Section(header: Text("Voti che devi prendere")){ ForEach(voti2, id: \.self){ item2 in Text("\(item2)") } } }.onReceive(timer) { input in voti2 = tutto() } .navigationTitle("Media") } } func tutto() - [Float] { let testInt = Int(test) ?? 0 let mediaFloat = Float(media) ?? 0 var mediaEffettiva = mediaEffFunzione(dim: nVoti) if mediaEffettiva mediaFloat { for i in 0..testInt - 2 { voti2[i] = Float(Int(mediaEffettiva + 1)) } let mediaEffettivaInt = Int(mediaEffettiva) let mediaFloatInt = Int(mediaFloat) var con: Int = 0 while(mediaEffettivaInt mediaFloatInt){ for j in nVoti..nVoti + testInt - 2{ voti[j] = voti2[j - nVoti]; } mediaEffettiva = mediaEffFunzione(dim: nVoti + testInt) if(mediaEffettiva mediaFloat){ voti2[con] += 0.5 } con = con + 1 if con = testInt { con = 0 } } } return voti2 } func removeRows(at offsets: IndexSet) { voti.remove(atOffsets: offsets) } func mediaEffFunzione(dim: Int) - Float { var somma: Float = 0 for i in voti{ somma = somma + i } return somma / Float(dim) } } Thank you very much
6
0
1.1k
Mar ’21
Index out of range, SwiftUI
Hello import SwiftUI enum When: String { case Today case Week case Month case Year } struct ChartView: View { var when: When @EnvironmentObject var millilitriInseritiModel: MillilitriInseritiModel var valoriAsseX: [String]{ if when == When.Week{ return ["M", "T", "W", "T", "F", "S", "S"] } else if when == When.Month{ return ["7", "14", "21", "28"] } else if when == When.Year{ return ["J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"] } return [] } var valoriAsseY: [Double]{ return [] } var dates: [Date] = [Date(), Date().addingTimeInterval(86400), Date().addingTimeInterval(86400 * 2)] @State var valori: [Double] = [1000, 2000, 3000, 1000, 2000, 1000, 2000, 3000, 1000, 2000, 3000] var altezzaRettangolo: [Double]?{ var altezze: [Double] = [] for i in 0..<valori.count{ altezze.append(valori[i]) } return altezze } @State var animation: Bool = false var body: some View{ HStack(alignment: .bottom, spacing: 8, content: { ForEach(valoriAsseX.indices){ i in VStack{ RoundedRectangle(cornerRadius: 3) .fill(LinearGradient(gradient: Gradient(colors: [Color.red, Color.blue]), startPoint: .top, endPoint: .bottom)) .frame(width: 40, height: animation ? CGFloat(altezzaRettangolo?[i] ?? 0) / 7 : 0) .animation(.easeInOut) Text(valoriAsseX[i]) .fontWeight(.semibold) .multilineTextAlignment(.leading) .onTapGesture { withAnimation{ valori[i] += 100 } } } } }) .padding() .onAppear { animation = true } .onDisappear { animation = false } } } struct ChartView_Previews: PreviewProvider { static var previews: some View { ChartView(when: When.Year) } } As you might notice, in the previews I set when to When.Year, that is crashing the app because the array valoriAsseX is bigger than the array altezzaRettangolo, so when I iterate it in the ForEach loop it crashes, I can't find any way to solve this, I tried an if let but it is crashing anyway, any ideas? Thank you
5
0
2.2k
Jun ’21
@AppStorage with Date in SwiftUI
Hello I want to be able to save Date in @AppStorage, and it works however I was wondering what was the difference between these two extensions, which one is better and why? extension Date: RawRepresentable { static var dateFormatter: DateFormatter = { let formatter = DateFormatter() formatter.dateStyle = .long return formatter }() public var rawValue: String { Date.dateFormatter.string(from: self) } public init?(rawValue: String) { self = Date.dateFormatter.date(from: rawValue) ?? Date() } } and extension Date: RawRepresentable { private static let formatter = ISO8601DateFormatter() public var rawValue: String { Date.formatter.string(from: self) } public init?(rawValue: String) { self = Date.formatter.date(from: rawValue) ?? Date() } } Thank You!
5
0
6.2k
Dec ’24
Looping views SwiftUI
Hello Is there a view to loop different views, for example in a ForEach loop Here is what I mean: import SwiftUI struct ContentView: View { &#9;&#9;@State var view1 = false &#9;&#9;@State var view2 = false &#9;&#9;@State var view3 = false &#9;&#9;@State var view4 = false &#9;&#9; &#9;&#9;private var gridItemLayout = [GridItem(.flexible()), GridItem(.flexible())] &#9;&#9; &#9;&#9; &#9;&#9;var textSize: CGFloat { &#9;&#9;&#9;&#9;if UIDevice.current.userInterfaceIdiom == .pad { &#9;&#9;&#9;&#9;&#9;&#9;return 48 &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;return 23 &#9;&#9;} &#9;&#9; &#9;&#9;var title: CGFloat { &#9;&#9;&#9;&#9;if UIDevice.current.userInterfaceIdiom == .pad { &#9;&#9;&#9;&#9;&#9;&#9;return 60 &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;return 34 &#9;&#9;} &#9;&#9; &#9;&#9;var height: CGFloat { &#9;&#9;&#9;&#9;if UIDevice.current.userInterfaceIdiom == .pad { &#9;&#9;&#9;&#9;&#9;&#9;return 0.15 &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;return 0.15 &#9;&#9;} &#9;&#9; &#9;&#9;var weight: CGFloat { &#9;&#9;&#9;&#9;if UIDevice.current.userInterfaceIdiom == .pad { &#9;&#9;&#9;&#9;&#9;&#9;return 0.44 &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;return 0.43 &#9;&#9;} &#9;&#9; &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;NavigationView{ &#9;&#9;&#9;&#9;&#9;&#9;GeometryReader { geometry in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;ScrollView(.vertical, showsIndicators: true) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;LazyVGrid(columns: gridItemLayout, spacing: 18){ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Group{ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text("View1") &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.foregroundColor(.black) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.frame(width: geometry.size.width * weight, height: geometry.size.height * height) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.background(Color.white) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.onTapGesture { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;view1 = true &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.sheet(isPresented: $view1) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;View1() &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text("View2") &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.foregroundColor(.black) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.frame(width: geometry.size.width * weight, height: geometry.size.height * height) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.background(Color.white) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.onTapGesture { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;view2 = true &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.sheet(isPresented: $view2) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;View2() &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text("View3") &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.foregroundColor(.black) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.frame(width: geometry.size.width * weight, height: geometry.size.height * height) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.background(Color.white) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.onTapGesture { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;view3 = true &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.sheet(isPresented: $view3) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;View3() &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text("View4") &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.foregroundColor(.black) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.frame(width: geometry.size.width * weight, height: geometry.size.height * height) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.background(Color.white) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.onTapGesture { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;view4 = true &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.sheet(isPresented: $view4) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;View4() &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;}.padding() &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;.navigationTitle("Title") &#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;} &#9;&#9;} &#9;&#9; &#9;&#9; } Is there a way to put the 4 views in a loop instead of making four different Text Views(they are different views) Thank your for your time
4
0
4.4k
Mar ’21
Notify when @FetchRequest changes?
Hello @FetchRequest( entity: Book(), sortDescriptors: [] ) var books: FetchedResults<Book> How can I get notified when books changes? I was thinking about putting this... .onReceive(NotificationCenter.default.publisher(for: .NSManagedObjectContextDidSave), perform: { _ in}) ... in my main View but this would notify me if anything gets saved and that is not what I want. I want to get notified just if a certain FetchRequest changes (in this case books)? How can I do that? Thank You!
4
1
2.6k
Jan ’23
Siri Shortcuts with SwiftUI and Core Data
Hello, I have created a simple SwiftUI app with Core Data and want to be able to add data via the shortcuts app, I have implemented Intents and the IntentHandler class. When I create a shortcut to add data to my app and run it, nothing happens in the app, the list does not refresh, the only way to see the added data is to close the app completely and reopen it. How can I refresh the UI immediately? I will post my Core Data stack and my SwiftUI view. struct PersistenceController { static let shared = PersistenceController() let container: NSPersistentContainer init() { container = NSPersistentContainer(name: "SiriShort") guard let fileContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.SiriShortcut2")?.appendingPathComponent("SiriShort.sqlite") else { fatalError("Shared file container could not be created.") } let storeDescription = NSPersistentStoreDescription(url: fileContainer) storeDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) storeDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey) container.persistentStoreDescriptions = [storeDescription] container.loadPersistentStores(completionHandler: { (storeDescription, error) in if let error = error as NSError? { fatalError("Unresolved error \(error), \(error.userInfo)") } }) container.viewContext.automaticallyMergesChangesFromParent = true container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy } } View: import SwiftUI import CoreData import Intents struct ContentView: View { @Environment(\.managedObjectContext) private var viewContext @State private var view: Bool = false @FetchRequest( sortDescriptors: [NSSortDescriptor(keyPath: \Item.text, ascending: true)], animation: .default) private var items: FetchedResults<Item> var body: some View { NavigationView { List { ForEach(items) { item in Text(item.text!) } .onDelete(perform: deleteItems) } .toolbar { ToolbarItem(placement: .navigationBarTrailing) { EditButton() } ToolbarItem { Button(action: addItem) { Label("Add Item", systemImage: "plus") } } } } } private func addItem() { withAnimation { let newItem = Item(context: viewContext) newItem.text = "\(Int.random(in: 0...1000))" do { try viewContext.save() } catch { // Replace this implementation with code to handle the error appropriately. // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. let nsError = error as NSError fatalError("Unresolved error \(nsError), \(nsError.userInfo)") } makeDonation(text: newItem.text!) } } func makeDonation(text: String) { let intent = MakeUppercaseIntent() intent.text = text intent.unit = "1" intent.suggestedInvocationPhrase = "Add \(text) to app" let interaction = INInteraction(intent: intent, response: nil) interaction.donate { (error) in if error != nil { if let error = error as NSError? { print("Donation failed: %@" + error.localizedDescription) } } else { print("Successfully donated interaction") } } } private func deleteItems(offsets: IndexSet) { withAnimation { offsets.map { items[$0] }.forEach(viewContext.delete) do { try viewContext.save() } catch { // Replace this implementation with code to handle the error appropriately. // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. let nsError = error as NSError fatalError("Unresolved error \(nsError), \(nsError.userInfo)") } } } }
4
0
2.2k
Jan ’22
DecimalPad Issues SwiftUI
Hello I have two questions: I'm using a decimalpad keyboard and when I use the comma the app doesn't recognise it How do I dismiss the keyboard? I want that in every country the decimals are written with the comma ( , ) (Can you please write the solution if you know it, thank you for your time) Code: import SwiftUI struct BMIView: View {          @State private var height = ""     @State private var weight = ""     @Environment(\.presentationMode) var presentationMode     var inputAfterConvertions: Float {          let hh = Float(height) ?? 0          let ww  = Float(weight) ?? 0          var ris: Float = 0          if hh > 0 && ww > 0{          ris = (ww / (hh * hh)) * 1000             return ris         }            return 0     }          var health: String{          if inputAfterConvertions < 18.49 && inputAfterConvertions > 0 { return ""         }         else if inputAfterConvertions > 18.5 && inputAfterConvertions < 24.99{             return ""         } else if inputAfterConvertions > 25{             return ""         }                  return ""     }     @State var isWriting: Bool = false     var body: some View {         NavigationView{             Form{                 Section(header: Text("Enter your height in cm")){                     TextField("Input",text: $height)                         .keyboardType(.decimalPad)                 }                 Section(header: Text("Enter your Weight in kg")){                     TextField("Input",text: $weight)                         .keyboardType(.decimalPad)                 }                 Section(header: Text("Check result")){                     Text("\(inputAfterConvertions, specifier: "%.2f")")                 }             }             .navigationBarTitle("BMI")             .navigationBarItems(trailing:                                     Button(action: {                                         presentationMode.wrappedValue.dismiss()                                     }) {                                         Image(systemName: "xmark").font(.title).foregroundColor(.blue)                                     }             )         }     } } Thank you very much for your time
3
0
5.4k
Jun ’21