gpt4 book ai didi

ios - 高级自动完成 Swift

转载 作者:行者123 更新时间:2023-11-28 15:46:35 27 4
gpt4 key购买 nike

我通过以下代码创建了一个简单的自动完成文本字段(其中自动完成选项显示在表格 View 中):

import UIKit

class SchoolViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {

@IBOutlet weak var schoolTextField: UITextField!
@IBOutlet weak var autoCompleteTableView: UITableView!

let schoolPossibilities = ["Redwood", "Fisher", "Bellermen", "Saratoga", "Los Gatos", "Cambell", "Mooreland", "Harker", "Challenger", "Saint Andrews", "Beckens", "Lynbrook", "Menlo", "Gunn", "Aragon", "Kipp"]
var autoCompleteSchools = [String]()

override func viewDidLoad() {
super.viewDidLoad()
autoCompleteTableView.delegate = self
schoolTextField.delegate = self
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "someCell", for: indexPath)
cell.textLabel?.text = autoCompleteSchools[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return autoCompleteSchools.count
}

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let text = schoolTextField.text{
let substring = (text as NSString).replacingCharacters(in: range, with: string)
searchAutoCompleteEntries(withSubstring: substring)
}
return true
}

func searchAutoCompleteEntries(withSubstring substring: String){
autoCompleteSchools.removeAll()
for key in schoolPossibilities{
let string = key as NSString
let range = string.range(of: substring)
if range.location == 0{
autoCompleteSchools.append(key)
}
}
autoCompleteTableView.reloadData()
}


}

问题是选项只有在文本字段中输入的内容完全匹配时才会显示。我如何更改此代码,以便它可以容忍大写和小写字母以及细微的变化?

最佳答案

中使用 NSCaseInsensitiveSearch 作为比较选项
outputString.rangeOfString(String, options: NSStringCompareOptions, range: <#T##Range<Index>?#>, locale: <#T##NSLocale?#>)

关于ios - 高级自动完成 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42941361/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com