Post Ai4zabWGboweZ2XqIS by uliwitness@chaos.social
(DIR) More posts by uliwitness@chaos.social
(DIR) Post #Ai4zaZK8lw3Zkqr87M by uliwitness@chaos.social
2024-04-22T18:05:38Z
0 likes, 0 repeats
Hey, is there any way to add a range check to a SwiftUI TextField?I have anstruct MyView: View { @ State var intValue: UInt16 var body: some View { TextField("UInt16”, value: $intValue, format: .number) }}and if I enter “-1” or “65537” into the text field, it calls fatalError() trying to convert the user-entered text into a UInt16, and I'd rather it just beep than crash the app on invalid user input.#swiftUI #swiftlang
(DIR) Post #Ai4zabWGboweZ2XqIS by uliwitness@chaos.social
2024-04-23T09:36:45Z
0 likes, 0 repeats
Update: I still don’t know how to correctly use this construct, it seems to just have never been tested after implementing it. Would still know if there’s a way to use this IRL.The onSubmit() workaround seems the least annoying, if inelegant (String→Int→String→Int). If you want to ship now, using a formatter instead seems like the quicker choice. In my case, a custom Binding was best, as I need to convert the value from storage anyway, so I don’t actually have a UInt16. #swiftUI #swiftlang