Tyler Wasick
Tyler Wasick

Reputation: 89

How to track the last entered character in a TextField?

I am creating a form and I need to track the last character entered. I know I can use onChange to detect when the text field changed, but it displays the while string and not just the last character. Any feedback is appreciated!

import SwiftUI

struct AddAssetView: View {
    @State private var costTextField = ""
        
    var body: some View {
        
        VStack {
            NavigationView {
                Form {
                    Section {
                        TextField("Cost", text: $costTextField)
                            .keyboardType(.numberPad)
                            .onChange(of: costTextField, perform: { value in
                                print(value)
                            })
                    }
                }.navigationTitle(Text("Enter a new item"))
            }
        }
    }
}

Upvotes: 0

Views: 637

Answers (1)

Claus Jørgensen
Claus Jørgensen

Reputation: 26336

What's wrong with just using at String.last ?

Upvotes: 0

Related Questions