Post

Replies

Boosts

Views

Activity

Reply to Change Placeholder to a String in a CustomTextField
The worst part of this issue exists on this line (line 32):     @Binding var value: Double Declaring the value as non-Optional, the textfield holds non-empty String in text and placeholder is not used. (In textfield.text = formatter.string(for: value) ?? placeholder, formatter.string(for: value) never generates nil when value is non-Optional Double.) Please try making the value as Double?: } struct DecimalTextField: UIViewRepresentable { &#9;&#9;private var placeholder: String &#9;&#9;@Binding var value: Double? //<- &#9;&#9;private var formatter: NumberFormatter &#9;&#9;init(_ placeholder: String, &#9;&#9;&#9;&#9; value: Binding<Double?>, //<- &#9;&#9;&#9;&#9; formatter: NumberFormatter ) { &#9;&#9;&#9;&#9;self.placeholder = placeholder &#9;&#9;&#9;&#9;self._value = value &#9;&#9;&#9;&#9;self.formatter = formatter &#9;&#9;} &#9;&#9;//... } And use it as: &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;DecimalTextField("Input", value: $height, formatter: currencyFormatter) //<- no `.bound` You can try changing line 61 as follows: textfield.text = value.map{formatter.string(for: $0)} ?? placeholder But I recommend you not to put placeholder into the property text. You will see why if you run it. You have no need to use bound any more, but the definition can be simplified: extension Optional where Wrapped == Double { &#9;&#9;var bound: Double { &#9;&#9;&#9;&#9;get{ &#9;&#9;&#9;&#9;&#9;&#9;return self ?? 0 &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;set { &#9;&#9;&#9;&#9;&#9;&#9;self = newValue &#9;&#9;&#9;&#9;} &#9;&#9;} }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20
Reply to Decoding JSON SwifUI
I recommend NEVER to use try?, which throws away all the error info. Change your line 79...83 as: &#9;&#9;let data: Data &#9;&#9;do { &#9;&#9;&#9;&#9;data = try Data(contentsOf: url) //<-`try`, not `try?` &#9;&#9;} catch { &#9;&#9;&#9;&#9;fatalError("Failed to load \(file) from bundle.: \(error)") &#9;&#9;} And line 95...99: &#9;&#9;&#9;&#9;let loaded: T &#9;&#9;&#9;&#9;do { &#9;&#9;&#9;&#9;&#9;&#9;loaded = try decoder.decode(T.self, from: data) //<-`try`, not `try?` &#9;&#9;&#9;&#9;} catch { &#9;&#9;&#9;&#9;&#9;&#9;fatalError("Failed to decode \(file) from bundle.: \(error)") &#9;&#9;&#9;&#9;} And please tell us what you get. That would show some info where to fix.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20
Reply to SwiftUI HitTesting cannot be disabled in ZStack
As far as I tried your code, both the button and the scroll view works under the Rectangle. (I needed to modify some parts to make the button visible and the content enough to scroll.) Your issue may or may not be a bug of SwiftUI. Anyway, you should better show enough code to reproduce the issue.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20
Reply to DecimalPad Issues SwiftUI
I'm using a decimalpad keyboard and when I use the comma the app doesn't recognise it I want that in every country the decimals are written with the comma ( , ) Don't you think it is better for users that they get localized, familiar keypad shown? Unless you are creating sort of a programming tool, decimal separator should be localized, I think. You need to do 3 things consistent, when you work with numeric values in UI. Show properly localized keypad (iOS will manage it, and I do not know how to override the behavior of iOS) Read the numeric values using NumberFormatter properly localized Show the numeric values using NumberFormatter properly localized struct BMIView: View { &#9;&#9;let numberFormatter: NumberFormatter = { &#9;&#9;&#9;&#9;let nf = NumberFormatter() &#9;&#9;&#9;&#9;nf.locale = Locale.current &#9;&#9;&#9;&#9;//Set up the NumberFormatter as you like... &#9;&#9;&#9;&#9;nf.numberStyle = .decimal &#9;&#9;&#9;&#9;nf.maximumFractionDigits = 1 &#9;&#9;&#9;&#9;return nf &#9;&#9;}() &#9;&#9; &#9;&#9;@State private var height = "" &#9;&#9;@State private var weight = "" &#9;&#9;@Environment(\.presentationMode) var presentationMode &#9;&#9;var inputAfterConvertions: Float { &#9;&#9;&#9;&#9;//Use NumberFormatter to read numeric values &#9;&#9;&#9;&#9;let hh = numberFormatter.number(from: height)?.floatValue ?? 0 //<- &#9;&#9;&#9;&#9;let ww&#9;= numberFormatter.number(from: weight)?.floatValue ?? 0 //<- &#9;&#9;&#9;&#9; var ris: Float = 0 &#9;&#9;&#9;&#9; if hh > 0 && ww > 0{ &#9;&#9;&#9;&#9; ris = (ww / (hh * hh)) * 1000 &#9;&#9;&#9;&#9;&#9;&#9;return ris &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9; return 0 &#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 height in cm")){ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;TextField("Input",text: $height) &#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;Section(header: Text("Enter your Weight in kg")){ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;TextField("Input",text: $weight) &#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;Section(header: Text("Check result")){ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;//Use NumberFormatter to show numeric values &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text("\(inputAfterConvertions as NSNumber, formatter: numberFormatter)") //<- &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;//... } Seems you are trying something called DecimalTextField (regarding issue #2), but you should better always have in mind localization issues unless you write U.S. only apps. How do I dismiss the keyboard? Sorry, but I do not know the right solution in pure SwiftUI. Maybe something like UIViewRepresentable (as found in DecimalTextField) would be needed. One more. If you already have a similar thread and it is unsolved, you should better consider continuing on the old thread. It is not good for the forums to have many threads unsolved.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20
Reply to Xcode requisiti
I don't know how to close it... Unfortunately, this site does not have a feature about closing or deleting threads. And you know you should not mark threads as SOLVED when it is not solved. So, you need to be careful about what you have posted and edit it while allowed (currently you can edit your post for one hour, I think.)
Dec ’20
Reply to Decoding JSON SwifUI
> got this:  Thread 1: Fatal error: Failed to decode sf.json from bundle.: keyNotFound(CodingKeys(stringValue: "id", intValue: nil), Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: \"id\", intValue: nil) (\"id\").", underlyingError: nil)) Thanks for trying. And the most important part of the error is: keyNotFound and "id". It represents your sf.json lacks "id" in the first element of the JSON array. I have tested your code (including my suggestions) with this sf.json and it worked without runtime errors. sf.json: [ &#9;{ &#9;&#9;"id": "all", &#9;&#9;"title": "All", &#9;&#9;"items": [ &#9;&#9;&#9;"square.and.arrow.up", &#9;&#9;&#9;"square.and.arrow.up.fill", &#9;&#9;&#9;"square.and.arrow.down" &#9;&#9;&#9;] &#9; } ] (I filled comma at the end of Line 3, as you see.) What is your current sf.json? Does it have an entry for "id"?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20
Reply to Decoding JSON SwifUI
This is the file now:  Apparently, it does not have "id" in any elements. You can remove id as a stored property from SF to make your file decodable, and add another computed property id to make it conform to Identifiable: struct SF: Codable, Identifiable { &#9;&#9;//var id: String //<- Remove this stored property &#9;&#9;var id: String {title} //<- Add this computed property &#9;&#9;var title: String &#9;&#9;var items: [String] } And I have another question about the decimalPad That is not for this thread. Please keep, one thread for one topic.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20
Reply to Unable to copy symbols from this device
this also give error with notification "The archive Xcode_12.xx.xip" is damaged and can't be expanded". It seems there has been some networking issue. Better try downloading again when your network environment is free enough. You can download Xcode12.3.xip from More Downloads page - https://developer.apple.com/download/more/ directly. Which may tell you more info about downloading.
Dec ’20
Reply to DateFormatter().string(from: ) return "2021-Dec" instead of "2020-Dec"
You should better show how you get date, and your current locale and timezone. In my environment: print(Locale.current) //->en_US (current) print(TimeZone.current) //->Asia/Tokyo (current) let date = Date() let dateFormatterPrint = DateFormatter() dateFormatterPrint.dateFormat = "YYYY-MM-dd" dateFormatterPrint.timeZone = TimeZone.current dateFormatterPrint.string(from: date) //->"2021-12-28" This is the first week of the week-year 2021. Fixed Formats - https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW13 year Y 1..n 1997 Year (in "Week of Year" based calendars). Normally the length specifies the padding, but for two letters it also specifies the maximum length. This year designation is used in ISO year-week calendar as defined by ISO 8601, but can be used in non-Gregorian based calendar systems where week date processing is desired. May not always be the same value as calendar year. Try using yyyy instead of YYYY: let date = Date() let dateFormatterPrint = DateFormatter() dateFormatterPrint.dateFormat = "yyyy-MM-dd" dateFormatterPrint.timeZone = TimeZone.current dateFormatterPrint.string(from: date) //->"2020-12-28"
Topic: App & System Services SubTopic: General Tags:
Dec ’20
Reply to Fetch JSON Data not working
I don't get any error You do not get any error because you are replacing error with empty result. In the line marked as <- in your code: &#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;} Please try changing the lines above as follows and tell us what you see: &#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(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;}
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’20