Post

Replies

Boosts

Views

Activity

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)&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)&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.2k
Apr ’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)&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.2k
Apr ’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
736
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
958
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.5k
Mar ’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 { 		 		let identifierLabel: UILabel = { 				let label = UILabel() 				label.backgroundColor = .white 				label.textAlignment = .center 				label.translatesAutoresizingMaskIntoConstraints = false 				return label 		}() 		override func viewDidLoad() { 				super.viewDidLoad() 				 				// here is where we start up the camera 				// for more details visit: https://www.letsbuildthatapp.com/course_video?id=1252 				let captureSession = AVCaptureSession() 				captureSession.sessionPreset = .photo 				 				guard let captureDevice = AVCaptureDevice.default(for: .video) else { return } 				guard let input = try? AVCaptureDeviceInput(device: captureDevice) else { return } 				captureSession.addInput(input) 				 				captureSession.startRunning() 				 				let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) 				view.layer.addSublayer(previewLayer) 				previewLayer.frame = view.frame 				 				let dataOutput = AVCaptureVideoDataOutput() 				dataOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "videoQueue")) 				captureSession.addOutput(dataOutput) 				 				 				 				setupIdentifierConfidenceLabel() 		} 		 		fileprivate func setupIdentifierConfidenceLabel() { 				view.addSubview(identifierLabel) 				identifierLabel.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -32).isActive = true 				identifierLabel.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true 				identifierLabel.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true 				identifierLabel.heightAnchor.constraint(equalToConstant: 50).isActive = true 		} 		 		func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { //				print("Camera was able to capture a frame:", Date()) 				 				guard let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return } 				 				// !!!Important 				// make sure to go download the models at https://developer.apple.com/machine-learning/ scroll to the bottom 				guard let model = try? VNCoreMLModel(for: Classfier().model) else { return } 				let request = VNCoreMLRequest(model: model) { (finishedReq, err) in 						 						//perhaps check the err 						 //						print(finishedReq.results) 						 						guard let results = finishedReq.results as? [VNClassificationObservation] else { return } 						 						guard let firstObservation = results.first else { return } 						 						print(firstObservation.identifier, firstObservation.confidence) 						 						DispatchQueue.main.async { 								self.identifierLabel.text = "\(firstObservation.identifier) \(firstObservation.confidence * 100)" 						} 						 				} 				 				try? VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:]).perform([request]) 		} } Thank you for your time
2
1
4.4k
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 { 		 		@ObservedObject var networkController = NetworkControllerItalia() 		var gridItemLayout = [GridItem(.flexible()), GridItem(.flexible())] 		 		var body: some View { 				NavigationView{ 						ScrollView{ 								ForEach(networkController.users, id: \.self){ user in 										GroupBox(label: Text(user.round ?? ""), content: { 												VStack{ 														Text(user.team1 ?? "") 												} 										}).padding() 						} 						.navigationTitle("Serie A") 				} 		 } 	 } } class NetworkControllerItalia: ObservableObject { 		private var can: AnyCancellable? 		 		let url = URL(string: "https://raw.githubusercontent.com/openfootball/football.json/master/2020-21/it.1.json")! 		//let url = URL(string: "google.com")! 		@Published var users = [UserItalia(matches: [])] 		 		init() { 				self.can = URLSession.shared.dataTaskPublisher(for: url) 						.map { $0.data } 						.decode(type: [UserItalia].self, decoder: JSONDecoder()) 						//.replaceError(with: []) 						.eraseToAnyPublisher() 						.receive(on: DispatchQueue.main) 						.sink(receiveCompletion: {completion in 								print(completion) 						}, receiveValue: { users in 								self.users	= users 						}) 		} 		 } struct UserItalia: Decodable, Hashable { 		var matches: [Matches]? } struct Matches: Decodable, Hashable { 		var round: String? 		var date: String? 		var team1: String? 		var team2: String? } Thank You
9
0
3.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.2k
Jan ’21
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
2.7k
Jan ’21
Convert with Measurement SwiftUI
Hello I'm using Measurement(...) to convert watts to femtowatts, but if I convert 1 watts to femtowatts I get 999999999999999.9 instead of 1e+15, I have no idea on how to fix it, any help or ideas, here is the code : import SwiftUI struct PowerView: View { &#9;&#9; &#9;&#9;@State private var inputValue = "" &#9;&#9; &#9;&#9; let inputUnits = [ &#9;&#9;&#9;&#9;"watts", &#9;&#9;&#9;&#9;"femtowatts" &#9;&#9;] &#9;&#9;let outputUnits = [ &#9;&#9;&#9;&#9;"watts", &#9;&#9;&#9;&#9;"femtowatts" &#9; ] &#9;&#9;@State private var inputUnitValue = 0 &#9;&#9; &#9;&#9;@State private var outputUnitValue = 1 &#9;&#9; &#9;&#9;var after: String{ &#9;&#9;&#9;&#9;var input = Measurement(value: 0, unit: UnitPower.watts) &#9;&#9;&#9;&#9;var output: String = "" &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;switch inputUnits[inputUnitValue] { &#9;&#9;&#9;&#9;case "watts": input = Measurement(value: Double(inputValue) ?? 0, unit: UnitPower.watts) &#9;&#9;&#9;&#9;case "femtowatts": input = Measurement(value: Double(inputValue) ?? 0, unit: UnitPower.femtowatts) &#9;&#9;&#9;&#9;default: input = Measurement(value: Double(inputValue) ?? 0, unit: UnitPower.watts) &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;switch outputUnits[outputUnitValue] { &#9;&#9;&#9;&#9;case "watts": output = String(describing: input.converted(to: UnitPower.watts)) &#9;&#9;&#9;&#9;case "femtowatts": output = String(describing: input.converted(to: UnitPower.femtowatts)) &#9;&#9;&#9;&#9;default: output = String(describing: input.converted(to: UnitPower.watts)) &#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;&#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;&#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; 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)") &#9;&#9;&#9;&#9;&#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("Pressure") &#9;&#9;&#9;&#9;} &#9;&#9;} &#9;&#9; } Thank you for your time (Is there a way to use NumberFormatter with the computed String)
1
0
1.1k
Dec ’20