我有一个文本字段,判断是在shouldChangeCharactersIn函数中完成的.
首先,我有一组允许的字符:
// Check characters
allowedCharacters = CharacterSet.decimalDigits.union(.letters)
allowedCharacters = allowedCharacters?.union(CharacterSet(charactersIn: "àÀáÁâÂãÃäÄåāÅæèÈéÉêÊëËìÌíÍîÎïÏòÒóÓöÖôÔõÕøØùÙúÚûÛüÜýÝÿçÇñÑ"))
allowedCharacters = allowedCharacters?.union(CharacterSet(charactersIn: " ,.:;@#%*+&_=<>!?\r\n'(){}[]/-"))
allowedCharacters变量现在保存我希望在应用程序中允许的所有字符.trimminCharacters删除不在允许集中的所有字符.
guard string.trimmingCharacters(in: allowedCharacters!) == "" else { return false }
这似乎工作正常,但当用户将键盘切换为土耳其语或中文时,可能会输入上面列表中没有的字符.
我想阻止所有不在允许的字符集中的字符.如何防止用户输入这些字符?