In order to format user input in UITextField
, we define a custom textField(_:shouldChangeCharactersIn:replacementString:)
method. Below you can see code similar to the one we use in production.
func textField(
_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String
) -> Bool {
let result = replaceCharacters(
inText: textField.text ?? "",
range: range,
withCharacters: string
)
let formattedResult = format(result)
textField.text = formattedResult
return false
}
While normal input works fine, when our users input text with a continuous swipe this code sends UITextField
into an infinite loop. You can see the resulting stacktrace below.
What are we doing wrong here?