ios - 在 UISearchBar 中过滤撇号(特殊字符) - Swift,Xcode
问题描述
我目前有一个工作正常的 UISearchBar,但是,我注意到在 searchBar 中输入撇号时,它不会返回任何结果。例如:如果我有一个字符串:Bob's如果我搜索Bob它会返回所述字符串,但是,只要我在 searchBar: Bob'中输入撇号,searchBar就不会返回任何结果。
我在网上搜索了解决方案,但是没有找到任何有效的方法。任何回应将不胜感激,谢谢。
UISearch条码:
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchText.isEmpty {
filteredData = data
} else {
filteredData = data.filter{$0.title.range(of: searchText, options: .caseInsensitive) != nil }
}
self.tableView.reloadData()
}
}
结构:
struct Category {
let title: String
let items: [String]
}
大批:
Category(title: "Bob's ", items: ["Data: xxx", "Data: xxx", "Data: xxx",]),
cellForRowAt 函数:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell
let title = filteredData[indexPath.row].title
cell.myLabel.text = title
cell.myImg.image = UIImage(named: title + ".png")
return cell
}
}
在 searchBar 中键入撇号时在控制台中返回的输出: filteredData = []
解决方案
这已解决。事实证明,撇号有多种变体。有 U+0027 (') 和 U+2019 (')。U+2019 (') 是 iPhone 上的默认撇号,适用于上面的 UISearchBar 代码。希望这可以帮助任何与我有相同问题的人。
推荐阅读
- python - Python 方法 URL 参数
- regex - 无法使用 Groovy 从文本文件中捕获所需的字符串 - Jmeter JSR223
- c# - 阻止 Nuget 将相对路径添加到 .csproj 文件
- python - 我想阻止 Discord Bot 运行
- google-apps-script - 尝试使用工作表作为 Excel 附件发送电子邮件时显示异常错误
- android - 如何允许访问我的网络之外的本地主机 wamp 服务器?
- c# - 找到对象并比较后获取值
- javascript - Javascript - 动态变量
- reactjs - 尝试使用 Tailwind 启动 React 应用程序失败
- pine-script - Pine 脚本 - 动态历史参考