uliwitness,
@uliwitness@chaos.social avatar

Hey, is there any way to add a range check to a SwiftUI TextField?

I have an

struct 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.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • SwiftUI
  • kavyap
  • thenastyranch
  • GTA5RPClips
  • tester
  • InstantRegret
  • DreamBathrooms
  • ngwrru68w68
  • magazineikmin
  • everett
  • Youngstown
  • mdbf
  • slotface
  • rosin
  • cisconetworking
  • megavids
  • khanakhh
  • normalnudes
  • osvaldo12
  • cubers
  • tacticalgear
  • Durango
  • ethstaker
  • modclub
  • anitta
  • provamag3
  • Leos
  • JUstTest
  • lostlight
  • All magazines