I'm new to developing and I am trying to limit the number of characters that can be entered in a textfield, with the most recent version of Swift. I have tried to follow several different tutorials and have checked out a few different answers around the site, but I am not having much luck.
Currently, I have this entered in my swift file:
@IBAction func InfoTextFieldLimit(sender: UITextField) {
self.InfoTextField.delegate = self
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let currentCharacterCount = textField.text?.characters.count ?? 0
if (range.length + range.location > currentCharacterCount) {
return false
}
let newLength = currentCharacterCount + string.characters.count - range.length
return newLength <= 10
}
}
...I'm not getting any errors, but nothing seems to be happening when I run the app.
I have also tried at least 3 other methods that I didn't bother copying. (but can provide on request)
Can anybody tell me what I'm doing wrong and point me in the right direction? Would be much appreciated! ...I feel like there's a good chance I might be overlooking something that's obvious.
I am also going to want to do this with a textview down the road, how would that be different?