1

I have used this delegate method but it's not returning last character which I entered.

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool

Eg. Current Text field string is "123" and when I enter "4", so textfield string is "1234" but above delegate method return only "123", now again entered "5" then above method return "1234" but I'm expecting "12345"

Ashvin
  • 8,227
  • 3
  • 36
  • 53

1 Answers1

2

But later I found the solution which works for me

Swift 5

 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        var finalString = ""
        if range.length > 0 {
            finalString = "\(textField.text!.dropLast())"
        } else {
            finalString = "\(textField.text! + string)"
        }
        print(finalString)
        return true
    }
Ashvin
  • 8,227
  • 3
  • 36
  • 53