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