2017-02-03 71 views
0

我試圖根據輸入到searchBar中的數字更新我的搜索結果。然而,當我使用此代碼查找searBar.text是否包含整數字符串?

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它不承認的數量。問題是我需要它爲我輸入的任何整數工作,因爲數字的範圍很大,我不能爲每個單獨的數字創建一個個案。

回答

0

你可以得到這樣

SWIFT 1

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

迅速2,3

if let intValue = Int(searchBar.text) { 

    if intValue > 0 { 

    } 

} 

希望這有助於你

int值
相關問題