ios - 查找searBar.text是否包含整数字符串?

标签 ios swift search filter int

我正在尝试根据在搜索栏中输入的数字来更新我的搜索结果。但是,当我使用此代码时,它无法识别该号码

switch searchBar.text! {
    case "", nil:
        inSearchMode = false
    case "\(Int)":
        filteredData = dataSource.data.filter({"\($0.genusNum!)" == self.searchBar.text! })
    default:
        inSearchMode = true
        filteredData = dataSource.data.filter({$0.identifier?.range(of: lower) != nil })
    }

但是,当我将 Int 替换为实际整数时,它会起作用。问题是我需要它适用于我输入的任何整数,因为数字范围很大,我无法为每个单独的数字提供案例。

最佳答案

你可以这样获取int值

快速1

if let intValue = searchBar.text.toInt() where intValue > 0 {
// do your stuff
}

swift 2和3

if let intValue = Int(searchBar.text) {

   if intValue > 0 {

   }

}

希望这对你有帮助

关于ios - 查找searBar.text是否包含整数字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42033060/

相关文章:

java - 创建一个 Java 程序来搜索文件中的特定单词

iOS Today 扩展 POST 请求到 Google Directions API

ios - 更改 UITableView 中的行高度会导致奇怪的故障/闪烁

ios - 如何从打印的结构中删除结构名称(swift4)

c# - 从 URL 获取图像

python - 在列表中搜索字符或字符串(如网站上的查找功能)

php mysql 搜索关键字的描述

ios - 在Xcode中运行应用程序时,如何通过mac传输屏幕和控制设备?

ios - tableView.reloadData() 结束其他动画,如何解决?

ios - Codename One原生接口(interface)如何实现NSLog?