How to limit the number of characters in Swiftui’s Textfield?

Asked

Viewed 23 times

0

In the code below removeLast() works on "textField text: $teste", but, does not work in the "text: $addCollectionVM.name".

The difference between them is that the first is @State and the second @stateObject. Please someone could give me a hint?

NavigationView {
            Form {
                TextField("Collection Name", text: $addCollectionVM.name)
                    .onChange(of: self.addCollectionVM.name, perform: { value in
                        if value.count > 2 {
                            self.addCollectionVM.name.removeLast()
                        }
                    })

                
                TextField("Collection description", text: $addCollectionVM.cDescription)
                
                TextField("Teste", text: $teste)
                    .onChange(of: self.teste, perform: { inputValue in
                        if inputValue.count > 2 {
                            self.addCollectionVM.name.removeLast()
                        }
                    })
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.