如果用户键入小写字母,Swift UITextField 将匹配字符串数组

标签 swift swift2

我有一个字符串数组,当用户在文本字段中键入内容时,我希望它检查该数组以查看是否存在匹配项。如果用户输入正确的大小写,这就可以正常工作……即。 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/

相关文章:

swift - 在 UITableView 中存储单个 UISwitch 状态

ios - 在 Swift 中的浏览器中打开 Web url 链接

ios - 如何在 Swift 中同步 tableview 和 searchbar 选择?

ios - UICollectionViewCell 约束问题

swift - 在 SwiftUI 中同时打开多个预览

ios - obj_msgSend CPU 使用率 100%

xcode - 如何在 Xcode 中将 CLLocation 转换为字符串

ios - 如何以编程方式在 ScrollView 中嵌入堆栈 View

ios - 如何对齐 UIImage 下面的 UIButton 中的文本,因为它没有正确显示

swift - 您可以在 Swift 中加载应用程序之前进行测试设置吗?