我有一个字符串数组,当用户在文本字段中键入内容时,我希望它检查该数组以查看是否存在匹配项。如果用户输入正确的大小写,这就可以正常工作……即。 Potter ...但如果用户输入 potter,它将找不到匹配项。我知道如何将它转换为小写,但我希望它在最后显示正确的大小写 Potter
这是我试过的
func tempConvertToLowerCase(){
let nowLowerCaseTextFieldInput = textField.text
nowLowerCaseTextFieldInput?.lowercaseString
var nowLowerCaseArrayEach : String?
for key in autoCompletePossibilities {
if key == textField.text?.lowercaseString {
nowLowerCaseArrayEach = key.lowercaseString
print ("it worked")
}
}
}
更新
我已经试过了,但我的代码仍然无法正常工作
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let substring = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)
if textField == textField{
let newText = range.length == 0 ? textField.text! + string : (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)
if autoCompletePossibilities.contains({$0.caseInsensitiveCompare(newText) == .OrderedSame}) {
print("it worked")
}
}
return true
}
最佳答案
试试这个:
if text.caseInsensitiveCompare(anotherText) == .OrderedSame
关于如果用户键入小写字母,Swift UITextField 将匹配字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37473258/