Post

Replies

Boosts

Views

Created

Cannot convert value of type 'String' to type 'NSNumber' in coercion
Hello I'm trying to use a formatter on a String but it is giving me this error Cannot convert value of type 'String' to type 'NSNumber' in coercion, is there a simple and short way to fix it? Here is the code import SwiftUI struct TemperatureView: View { &#9;&#9; &#9;&#9;@State private var inputValue = "" &#9;&#9; &#9;&#9; let inputUnits = [ &#9;&#9;&#9;&#9;"celsius [°C]", &#9;&#9;&#9;&#9;"kelvin [K]", &#9;&#9;&#9;&#9;"fahrenheit [°F]" &#9;&#9;] &#9;&#9;let outputUnits = [ &#9;&#9;&#9;&#9;"celsius [°C]", &#9;&#9;&#9;&#9;"kelvin [K]", &#9;&#9;&#9;&#9;"fahrenheit [°F]" &#9; ] &#9;&#9;@State private var inputUnitValue = 0 &#9;&#9; &#9;&#9;@State private var outputUnitValue = 1 &#9;&#9; &#9;&#9; &#9;&#9;var after: String{ &#9;&#9;&#9;&#9;var input: Measurement<UnitTemperature> &#9;&#9;&#9;&#9;var output: String = "" &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;switch inputUnits[inputUnitValue] { &#9;&#9;&#9;&#9;case "celsius [°C]": input = Measurement(value: Double(inputValue) ?? 0, unit: .celsius) &#9;&#9;&#9;&#9;case "kelvin [K]": input = Measurement(value: Double(inputValue) ?? 0, unit: .kelvin) &#9;&#9;&#9;&#9;case "fahrenheit [°F]": input = Measurement(value: Double(inputValue) ?? 0, unit: .fahrenheit) &#9;&#9;&#9;&#9;default: input = Measurement(value: Double(inputValue) ?? 0, unit: UnitTemperature.celsius) &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;switch outputUnits[outputUnitValue] { &#9;&#9;&#9;&#9;case "celsius [°C]": output = outputFormatter.string(from: input.converted(to: .celsius)) &#9;&#9;&#9;&#9;case "kelvin [K]": output = outputFormatter.string(from: input.converted(to: .kelvin)) &#9;&#9;&#9;&#9;case "fahrenheit [°F]": output = outputFormatter.string(from: input.converted(to: .fahrenheit)) &#9;&#9;&#9;&#9;default: output = String(describing: input.converted(to: UnitTemperature.celsius)) &#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;return output &#9;&#9;} &#9;&#9; &#9;&#9;@Environment(\.presentationMode) var presentationMode &#9;&#9;let outputFormatter: MeasurementFormatter = { &#9;&#9;&#9;&#9;&#9;&#9;let nf = NumberFormatter() &#9;&#9;&#9;&#9;&#9;&#9;nf.locale = Locale.current &#9;&#9;&#9;&#9;&#9;&#9;nf.usesSignificantDigits = true &#9;&#9;&#9;&#9;&#9;&#9;let mf = MeasurementFormatter() &#9;&#9;&#9;&#9;&#9;&#9;mf.numberFormatter = nf &#9;&#9;&#9;&#9;&#9;&#9;mf.unitOptions = .providedUnit &#9;&#9;&#9;&#9;&#9;&#9;return mf &#9;&#9;&#9;&#9;}() &#9;&#9;&#9;&#9; &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;NavigationView{ &#9;&#9;&#9;&#9;&#9;&#9;Form{ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Section(header: Text("Enter your Input value")){ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;TextField("Have a goal?", text: $inputValue) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.keyboardType(.decimalPad) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Section(header: Text("Input")){ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Picker("Input values", selection: $inputUnitValue){ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;ForEach(0..<inputUnits.count){ item in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text(inputUnits[item]) &#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;Section(header: Text("Output")){ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Picker("Output values", selection: $outputUnitValue){ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;ForEach(0..<outputUnits.count){ item in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text(outputUnits[item]) &#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;Section(header: Text("Check your Output value")){ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text("\(after as NSNumber, formatter: outputFormatter)") &#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;.navigationBarTitle("Temperature") &#9;&#9;&#9;&#9;&#9;&#9;.navigationBarItems(trailing: &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Button(action: { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;presentationMode.wrappedValue.dismiss() &#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;Image(systemName: "xmark").font(.title).foregroundColor(.blue) &#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; } The error is at line 80/81 Thank you for your time
1
0
3k
Jan ’21
Change background SwiftUI
Hello I made a custom Picker View but I don't have any idea on how to change the background color to grey when I press on one of the options Here is the code: struct PickerView: View { &#9;&#9; &#9;&#9;var arr: [String] = ["Easy", "Medium", "Hard"] &#9;&#9;var h: CGFloat = 50 &#9;&#9;var w: CGFloat = 320 &#9;&#9; &#9;&#9;@ObservedObject var input: UserInput &#9;&#9;var body: some View{ &#9;&#9;&#9;&#9;HStack(spacing: 40){ &#9;&#9;&#9;&#9;&#9;&#9;ForEach(0..<arr.count){ i in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;HStack(spacing: 25){ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text(arr[i]) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.bold() &#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;switch i{ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;case i: input.indi = i &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;default: return &#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;print(i) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;if(i < arr.count - 1){ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Divider() &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.frame(height: 25) &#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;.clipShape(RoundedRectangle(cornerRadius: 16, style: .continuous)) &#9;&#9;&#9;&#9;.overlay( &#9;&#9;&#9;&#9;&#9;&#9;RoundedRectangle(cornerRadius: 16) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.stroke(Color.gray, lineWidth: 3) &#9;&#9;&#9;&#9;) &#9;&#9;} } class UserInput: ObservableObject { &#9;&#9;@Published var indi: Int = 0 } Thank you for your time
7
0
3.6k
Jan ’21
Fetching from JSON SwiftUI
Hello I'm trying to fetch some data from a JSON Api, but when I access the properties of my struct it tells me Value of Type "Name Of The Struct" has no member "name of the property" Any idea on how to fix it Code: import SwiftUI import Combine struct ContentView: View { &#9;&#9; &#9;&#9;@ObservedObject var networkController = NetworkControllerItalia() &#9;&#9;var gridItemLayout = [GridItem(.flexible()), GridItem(.flexible())] &#9;&#9; &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;NavigationView{ &#9;&#9;&#9;&#9;&#9;&#9;ScrollView{ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;ForEach(networkController.users, id: \.self){ user in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;GroupBox(label: Text(user.round ?? ""), content: { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;VStack{ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text(user.team1 ?? "") &#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;.navigationTitle("Serie A") &#9;&#9;&#9;&#9;} &#9;&#9; } &#9; } } class NetworkControllerItalia: ObservableObject { &#9;&#9;private var can: AnyCancellable? &#9;&#9; &#9;&#9;let url = URL(string: "https://raw.githubusercontent.com/openfootball/football.json/master/2020-21/it.1.json")! &#9;&#9;//let url = URL(string: "google.com")! &#9;&#9;@Published var users = [UserItalia(matches: [])] &#9;&#9; &#9;&#9;init() { &#9;&#9;&#9;&#9;self.can = URLSession.shared.dataTaskPublisher(for: url) &#9;&#9;&#9;&#9;&#9;&#9;.map { $0.data } &#9;&#9;&#9;&#9;&#9;&#9;.decode(type: [UserItalia].self, decoder: JSONDecoder()) &#9;&#9;&#9;&#9;&#9;&#9;//.replaceError(with: []) &#9;&#9;&#9;&#9;&#9;&#9;.eraseToAnyPublisher() &#9;&#9;&#9;&#9;&#9;&#9;.receive(on: DispatchQueue.main) &#9;&#9;&#9;&#9;&#9;&#9;.sink(receiveCompletion: {completion in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;print(completion) &#9;&#9;&#9;&#9;&#9;&#9;}, receiveValue: { users in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.users&#9;= users &#9;&#9;&#9;&#9;&#9;&#9;}) &#9;&#9;} &#9;&#9; } struct UserItalia: Decodable, Hashable { &#9;&#9;var matches: [Matches]? } struct Matches: Decodable, Hashable { &#9;&#9;var round: String? &#9;&#9;var date: String? &#9;&#9;var team1: String? &#9;&#9;var team2: String? } Thank You
9
0
3.6k
Jan ’21
'init()' is deprecated: Use init(configuration:) instead and handle errors appropriately.
Hello I'm trying to detect objects with CreateML, but it gives me this warning that I think is breaking my app: 'init()' is deprecated: Use init(configuration:) instead and handle errors appropriately. The classfier.model is a CreatML model that has some images Have any ideas on how to fix it? import UIKit import AVKit import Vision import CoreML class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate { &#9;&#9; &#9;&#9;let identifierLabel: UILabel = { &#9;&#9;&#9;&#9;let label = UILabel() &#9;&#9;&#9;&#9;label.backgroundColor = .white &#9;&#9;&#9;&#9;label.textAlignment = .center &#9;&#9;&#9;&#9;label.translatesAutoresizingMaskIntoConstraints = false &#9;&#9;&#9;&#9;return label &#9;&#9;}() &#9;&#9;override func viewDidLoad() { &#9;&#9;&#9;&#9;super.viewDidLoad() &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;// here is where we start up the camera &#9;&#9;&#9;&#9;// for more details visit: https://www.letsbuildthatapp.com/course_video?id=1252 &#9;&#9;&#9;&#9;let captureSession = AVCaptureSession() &#9;&#9;&#9;&#9;captureSession.sessionPreset = .photo &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;guard let captureDevice = AVCaptureDevice.default(for: .video) else { return } &#9;&#9;&#9;&#9;guard let input = try? AVCaptureDeviceInput(device: captureDevice) else { return } &#9;&#9;&#9;&#9;captureSession.addInput(input) &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;captureSession.startRunning() &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) &#9;&#9;&#9;&#9;view.layer.addSublayer(previewLayer) &#9;&#9;&#9;&#9;previewLayer.frame = view.frame &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;let dataOutput = AVCaptureVideoDataOutput() &#9;&#9;&#9;&#9;dataOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "videoQueue")) &#9;&#9;&#9;&#9;captureSession.addOutput(dataOutput) &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;setupIdentifierConfidenceLabel() &#9;&#9;} &#9;&#9; &#9;&#9;fileprivate func setupIdentifierConfidenceLabel() { &#9;&#9;&#9;&#9;view.addSubview(identifierLabel) &#9;&#9;&#9;&#9;identifierLabel.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -32).isActive = true &#9;&#9;&#9;&#9;identifierLabel.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true &#9;&#9;&#9;&#9;identifierLabel.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true &#9;&#9;&#9;&#9;identifierLabel.heightAnchor.constraint(equalToConstant: 50).isActive = true &#9;&#9;} &#9;&#9; &#9;&#9;func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { //&#9;&#9;&#9;&#9;print("Camera was able to capture a frame:", Date()) &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;guard let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return } &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;// !!!Important &#9;&#9;&#9;&#9;// make sure to go download the models at https://developer.apple.com/machine-learning/ scroll to the bottom &#9;&#9;&#9;&#9;guard let model = try? VNCoreMLModel(for: Classfier().model) else { return } &#9;&#9;&#9;&#9;let request = VNCoreMLRequest(model: model) { (finishedReq, err) in &#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;//perhaps check the err &#9;&#9;&#9;&#9;&#9;&#9; //&#9;&#9;&#9;&#9;&#9;&#9;print(finishedReq.results) &#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;guard let results = finishedReq.results as? [VNClassificationObservation] else { return } &#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;guard let firstObservation = results.first else { return } &#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;print(firstObservation.identifier, firstObservation.confidence) &#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;DispatchQueue.main.async { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.identifierLabel.text = "\(firstObservation.identifier) \(firstObservation.confidence * 100)" &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;try? VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:]).perform([request]) &#9;&#9;} } Thank you for your time
2
1
4.9k
Jan ’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.6k
Mar ’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
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
852
Mar ’21
Problems with ForEach, SwiftUI
Hello I am getting a lot of errors in ForEach and I don't know why at all. Here is the code: The error is at line 12 import SwiftUI import Combine struct ContentView: View { @EnvironmentObject var networkController: NetworkControllerItalia var body: some View { Form{ TextField("Input city name", text: $networkController.cityName) Section { ForEach(networkController.users.weather, id: \.self){ user in } } } } } class NetworkControllerItalia: ObservableObject { private var can: AnyCancellable? @Published var cityName: String = "" @Published var users = [UserItalia(weather: Weather())] init(cityName: String) { self.cityName = cityName let url = URL(string: "http://api.openweathermap.org/data/2.5/weather?q=\(cityName)&amp;appid=")! self.can = URLSession.shared.dataTaskPublisher(for: url) .map { $0.data } .decode(type: [UserItalia].self, decoder: JSONDecoder()) .eraseToAnyPublisher() .receive(on: DispatchQueue.main) .sink(receiveCompletion: {completion in print(completion) }, receiveValue: { users in self.users = users }) } } struct UserItalia: Decodable, Hashable{ var weather: Weather } struct Weather: Decodable, Hashable { var main: String? } Thank you
3
0
2.6k
Apr ’21
Errors in ForEach SwiftUI
Hello I get errors in ForEach that I don't know how to solve: Generic struct 'ForEach' requires that 'Main' conform to 'RandomAccessCollection' Unable to infer type of a closure parameter 'user' in the current context Error at line 30 - 31 Code: // // ContentView.swift // ARWeather // // Created by Jad Taljabini on 08/04/21. // import SwiftUI import Combine struct ContentView: View { @EnvironmentObject var networkController: NetworkControllerItalia var body: some View { NavigationView { Form{ TextField("Input city name", text: $networkController.cityName, onEditingChanged: { te_xt in networkController.url = URL(string: "https://api.openweathermap.org/data/2.5/weather?q=\(networkController.cityName)&amp;appid=") ?? URL(string: "https://www.apple.com")! networkController.fun() }, onCommit: { withAnimation{ networkController.url = URL(string: "https://api.openweathermap.org/data/2.5/weather?q=\(networkController.cityName)&amp;appid=") ?? URL(string: "https://www.apple.com")! networkController.fun() } }) Section { ForEach(networkController.users.weather ?? [], id: \.self){ user in Text(user.main ?? "") } ForEach(networkController.users.main ?? Main(temp: 0), id: \.self){ user in Text("\(user.temp)") } } } } } } class NetworkControllerItalia: ObservableObject { private var can: AnyCancellable? @Published var cityName: String = "" @Published var users = UserItalia(weather: []) var url = URL(string: "https://www.apple.com")! func fun(){ self.can = URLSession.shared.dataTaskPublisher(for: url) .map { $0.data } .decode(type: UserItalia.self, decoder: JSONDecoder()) .eraseToAnyPublisher() .receive(on: DispatchQueue.main) .sink(receiveCompletion: {completion in print(completion) }, receiveValue: { users in self.users = users }) }//Funzione che riempe users di dati da internet } struct UserItalia: Decodable, Hashable{ var weather: [Weather]? var main: Main? } struct Weather: Decodable, Hashable { var main: String? } struct Main: Decodable, Hashable { var temp: Float } The JSON API is like this: { "coord": { "lon": -0.1257, "lat": 51.5085 }, "weather": [ { "id": 801, "main": "Clouds", "description": "few clouds", "icon": "02d" } ], "base": "stations", "main": { "temp": 285.45, "feels_like": 283.96, "temp_min": 284.82, "temp_max": 285.93, "pressure": 1021, "humidity": 47 }, "visibility": 10000, "wind": { "speed": 5.66, "deg": 220 }, "clouds": { "all": 20 }, "dt": 1617888145, "sys": { "type": 1, "id": 1414, "country": "GB", "sunrise": 1617859180, "sunset": 1617907473 }, "timezone": 3600, "id": 2643743, "name": "London", "cod": 200 } Thank you
3
0
3.6k
Apr ’21
Cannot use instance member 'name' within property initializer; property initializers run before 'self' is available
Hello Why does it give me an error when I pass 'name' at line 3? struct OtherView: View { @State private var name: String = "" @ObservedObject var use = Use(name: name) var body: some View{ VStack{ } } } class Use: ObservableObject { @Published var name: String init(name: String) { self.name = name } } Thank you
2
0
4.4k
Apr ’21
Touch and Face ID in SwiftUI
Hello Is there a way to know if the detected finger or face are wrong? I am using this function: func authenticate() { let context = LAContext() var error: NSError? if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &amp;error) { let reason = "We need to unlock your data." context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, authenticationError in DispatchQueue.main.async { if success { self.isUnlocked = true } else { userPressedCancel = false } } } } else { } }
1
0
943
Apr ’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