Decimal128 value not persisting

Per this thread, it appears there is a bug when using a binding from string to decimal with a text field, where the value does not get stored:

TextField("creditHours", value: $course.creditHours, formatter: NumberFormatter())
// also occurs if you pass in your own formatter such as:
let formatter: NumberFormatter = {
  let f = NumberFormatter()
  f.numberStyle = .decimal
  return f

The “working” suggestion on the bottom of that thread is to listen to the onChange event and manually persist it.

    @State var hours: String = ""

TextField("hours", text: $hours).onChange(of: hours) { val in
         if let dec = Double(val) {
             try! realm.write {
                 var c = course.thaw()!
                 c.creditHours = try! Decimal128(string: val)

However, the value still does not persist to the database.

Given Decimal128 is a realm swift class, I’m not sure how to diagnose further.

If I change the type to Double, then it works.

@State var hours: String = "2"

TextField("creditHours", text: $hours).onChange(of: hours) { val in
         if let d = Double(val) {
             try! realm.write {
                 var c = course.thaw()!
                 c.creditHours = d

Updating the value to 24 in the UI results in 24 saved to the backend.

Did you add a breakpoint to that line and a) ensure the code execution actually gets to that line and then b ) evaluate val to see what it resolves to before attempting to init the Decimal128 with it? Does anything get stored in that property in the database?

This code works

someProperty = try ! Decimal128(string: "9.99")

and even this code “works” but writes NaN to the property

someProperty = try ! Decimal128(string: "$9.99")