I'm currently coding the sign up part of my app. I would like to replace automatically spaces (in the username) by underscore..
I'm still new with swift and I don't know how to do that.
Currently, the code looks like that:
let ACCEPTABLE_CHARACTERS = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_."
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let cs = CharacterSet(charactersIn: ACCEPTABLE_CHARACTERS).inverted
let filtered: String = (string.components(separatedBy: cs) as NSArray).componentsJoined(by: "")
return (string == filtered)
}
Only characters in %ACCEPTABLE_CHARACTERS% are accepted. I would like to add a line to replace as I said, spaces by "_" directly.
Do you have any idea about how I could do this?