SwiftUI error when using NumberFormatter

Hello,

I do not understand something in my code bellow:

@Observable
class Dog: Identifiable
{
    var name = "Test"
    var age = 3
}

struct ContentView: View {
    @State private var model = Dog()
    let nf = NumberFormatter()
    
    var body: some View {
        Form
        {
            Section
            {
                TextField("Name", text: $model.name)
            }
            Section
            {
                TextField("Age", text: $model.age, formatter: nf)
            }
        }
    }
}

I have a compilation error: "Trailing closure passed to parameter of type 'FormStyleConfiguration' that does not accept a closure"

If i remove the age TextField, everything works fine.

Do you have any idea ? Maybe i have made something wrong with text formatter ?

Thanks

Answered by workingdogintokyo in 769582022

use TextField("Age", value: $model.age, formatter: nf). See the docs for the differences between the two versions, TextField, in particular the text version does not take a formatter

Accepted Answer

use TextField("Age", value: $model.age, formatter: nf). See the docs for the differences between the two versions, TextField, in particular the text version does not take a formatter

SwiftUI error when using NumberFormatter
 
 
Q