Post

Replies

Boosts

Views

Activity

Having issues with .sheet and .dismiss()
Hello When I dismiss my .sheet it is very slow and sometimes it doesn't work .sheet(isPresented: $myViewi) {         MyView()       } @Environment(\.presentationMode) var presentationMode .navigationBarItems(trailing:           Button(action: {    presentationMode.wrappedValue.dismiss()                                         }) {                                             Image(systemName: "xmark").font(.title).foregroundColor(.blue)                                         }                 ) How do I fix it? Thank You
2
0
920
Dec ’20
Decoding JSON SwifUI
Hello I'm trying to decode a JSON File but it's not working and I can't understand why Code: import SwiftUI struct Search: View {          let sf: [SF] = Bundle.main.decode("sf.json")     @State private var searchText: String = ""     var body: some View {         NavigationView{             Form {                 Section(header: Text("Search bar")){                 TextField("Search", text: $searchText)                     .padding(7)                     .background(Color(.systemGray6))                     .cornerRadius(8)                 }                 Section(){                     List{                         ForEach(sf) { item in                             Text(item.title)                         }                     }                 }             }             .navigationTitle("SF")         }     }      } struct SF: Codable, Identifiable {     var id: String     var title: String     var items: [String] } extension Bundle {   func decode<T: Codable>(_ file: String) -> T {          guard let url = self.url(forResource: file, withExtension: nil) else {       fatalError("Failed to locate \(file) in bundle.")     }               guard let data = try? Data(contentsOf: url) else {       fatalError("Failed to load \(file) from bundle.")     }               let decoder = JSONDecoder()              guard let loaded = try? decoder.decode(T.self, from: data) else {       fatalError("Failed to decode \(file) from bundle.")     }              return loaded   } } Here is the JSON file [   {     "id": "all"     "title": "All",     "items": [       "square.and.arrow.up",       "square.and.arrow.up.fill",       "square.and.arrow.down"       ]    } ] Thank you
10
0
3.8k
Dec ’20
Instance member 'deci' cannot be used on type 'BMIView'; did you mean to use a value of this type instead?
Hello How di i fix the error at line 16: I nstance member 'deci' cannot be used on type 'BMIView'; did you mean to use a value of this type instead?  import SwiftUI  struct BMIView: View {           @State var deci = "3"           var numberFormatter: NumberFormatter = {          let nf = NumberFormatter()          nf.locale = Locale.current          nf.numberStyle = .decimal          nf.maximumFractionDigits = Int(deci) ?? 0          return nf      }()            @State private var height = ""      @State private var weight = ""      @Environment(\.presentationMode) var presentationMode            var inputAfterConvertions: Double {          //Use NumberFormatter to read numeric values          let hh = numberFormatter.number(from: height)?.doubleValue ?? 0 //<-          let ww  = numberFormatter.number(from: weight)?.doubleValue ?? 0 //<-          var ris: Double = 0          if hh > 0 && ww > 0{              ris = (ww / (hh * hh)) * 10000              return ris          }          return 0      }           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 as NSNumber, formatter: numberFormatter)") //<-                  }              }              .navigationTitle("BMI")          }      }  } Thank you
2
0
849
Dec ’20
List in ForEach SwiftUI
Hello I don't know why but when I put a List before my ForEach loop, all the text disappears Code: // import SwiftUI struct SearchDetailView: View {     var sf: SF     var body: some View {         ScrollView{             ForEach(sf.items, id: \.self) { item in                 HStack{                 Image(systemName: item)                 Text(item)                 }             }             .navigationTitle(sf.title)         }     } } Thank you for your time
1
0
1.7k
Dec ’20
Change Placeholder to a String in a CustomTextField
Hello How do I make the default placeholder of a CustomTextField (that has a double binding) a string (onAppear) When I run the app on the TextField I see 0 instead of "Input" Code: import SwiftUI struct BMIView: View {     var currencyFormatter: NumberFormatter {         let formatter = NumberFormatter()         formatter.locale = .current         formatter.numberStyle = .decimal         return formatter     }     @State private var height: Double?         var body: some View {         NavigationView{             Form{                 Section(header: Text("Enter your height in cm")){                     DecimalTextField("Input", value: $height.bound, formatter: currencyFormatter)                }             }             .navigationBarTitle("BMI")         }     } } struct DecimalTextField: UIViewRepresentable {     private var placeholder: String     @Binding var value: Double     private var formatter: NumberFormatter     init(_ placeholder: String,          value: Binding<Double>,          formatter: NumberFormatter ) {         self.placeholder = placeholder         self._value = value         self.formatter = formatter     }     func makeUIView(context: Context) -> UITextField {         let textfield = UITextField()         textfield.keyboardType = .decimalPad         textfield.delegate = context.coordinator         textfield.placeholder = placeholder         textfield.text = formatter.string(for: value) ?? placeholder         textfield.textAlignment = .left         let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: textfield.frame.size.width, height: 44)) let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(textfield.doneButtonTapped(button:)))         let space = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace,                                     target: nil,action: nil)         toolBar.setItems([space, doneButton], animated: true)         textfield.inputAccessoryView = toolBar         return textfield     }     func updateUIView(_ uiView: UITextField, context: Context) {         // Do nothing, needed for protocol     }     func makeCoordinator() -> Coordinator {         Coordinator(self)     }     class Coordinator: NSObject, UITextFieldDelegate {         var parent: DecimalTextField         init(_ textField: DecimalTextField) {             self.parent = textField         }         func textField(_ textField: UITextField,                        shouldChangeCharactersIn range: NSRange,                        replacementString string: String) -> Bool {             // Allow only numbers and decimal characters             let isNumber = CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: string))             let withDecimal = (                 string == NumberFormatter().decimalSeparator &&                     textField.text?.contains(string) == false             )             if isNumber || withDecimal,                 let currentValue = textField.text as NSString?             {                 // Update Value                 let proposedValue = currentValue.replacingCharacters(in: range, with: string) as String                 let decimalFormatter = NumberFormatter()                 decimalFormatter.locale = Locale.current                 decimalFormatter.numberStyle = .decimal                 // Try currency formatter then Decimal formatrer                 let number = self.parent.formatter.number(from: proposedValue) ?? decimalFormatter.number(from: proposedValue) ?? 0.0                 // Set Value                 let double = number.doubleValue                 self.parent.value = double             }             return isNumber || withDecimal         }         func textFieldDidEndEditing(_ textField: UITextField,                                     reason: UITextField.DidEndEditingReason) {             // Format value with formatter at End Editing             textField.text = self.parent.formatter.string(for: self.parent.value)         }     } } // MARK: extension for done button extension  UITextField{     @objc func doneButtonTapped(button:UIBarButtonItem) -> Void {         self.resignFirstResponder()     } } extension Optional where Wrapped == Double {          var _bound: Double? {         get{             return self                      }         set{             self = newValue                      }     }          var bound: Double {         get{             return _bound ?? 0         }         set {             _bound = newValue         }     } } The problem might be at line 243 (I found the struct DecimalTextField on the internet) Thank you for your time
2
0
1.6k
Dec ’20
Fetch JSON Data not working
Hello I don't get any error when I fetch data but when I run I don't see any data Thank you import SwiftUI import Combine struct ContentView: View { &#9;&#9;@ObservedObject var networkController = NetworkController() &#9;&#9;@State var search: String = "" &#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(){ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;TextField("Search", text: $search) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Section(){ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;List(networkController.users.filter { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;$0.state.contains(search) } &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; , id: \.state){ user in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text(user.state) &#9;&#9;&#9;&#9;&#9;&#9;&#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("API") &#9;&#9;&#9;&#9;} &#9;&#9;} } class NetworkController: ObservableObject { &#9;&#9;private var can: AnyCancellable? &#9;&#9; &#9;&#9;let url = URL(string: "https://api.covidtracking.com/v1/states/current.json")! &#9;&#9;@Published var users = [User(state: "")] &#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: [User].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(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 User: Decodable { &#9;&#9;var state: String }
11
0
2.7k
Dec ’20
Attribute being read has no value: 768444
Hello When I tap on my TextField more than 2/4 times the app crashes Before that I get these errors: 2020-12-29 11:28:10.265525+0100 Nums[1670:295112] Can't find keyplane that supports type 4 for keyboard iPhone-PortraitChoco-NumberPad; using 25889PortraitChocoiPhone-Simple-PadDefault 2020-12-29 11:28:18.778004+0100 Nums[1670:295112] Can't find keyplane that supports type 4 for keyboard iPhone-PortraitChoco-NumberPad; using 25889PortraitChocoiPhone-Simple-PadDefault 2020-12-29 11:28:22.268833+0100 Nums[1670:295112] Can't find keyplane that supports type 4 for keyboard iPhone-PortraitChoco-NumberPad; using 25889PortraitChocoiPhone-Simple-Pad_Default Then after 2/4 four times tapping on the TextField the app crashes and I get this error 2020-12-29 11:28:22.419634+0100 Nums[1670:295112] [error] precondition failure: attribute being read has no value: 768444 AttributeGraph precondition failure: attribute being read has no value: 768444. (lldb)  Here is the TextField: GroupBox { &#9;&#9;&#9;&#9;&#9;&#9;HStack { &#9;&#9;&#9;&#9;&#9;&#9;&#9; TextField("Have a goal?", text: $saveGoal.bound) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; .keyboardType(.numberPad) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Spacer() &#9;&#9;&#9;&#9;&#9;&#9;Button(action: { &#9;&#9;&#9;&#9;&#9;&#9;UserDefaults.standard.set(self.saveGoal, forKey: "Save")&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#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("Save") &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#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(.top).padding(.trailing).padding(.leading) Thank you for your time
13
0
1.5k
Dec ’20
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.3k
Dec ’20
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