2017-04-16 51 views
0

我試圖過濾我的搜索欄,但我遇到了問題。我也跟着教程和複製並粘貼此代碼,但我得到一個錯誤「表達的類型是沒有更多的背景下曖昧」從表達式類型不明確,沒有更多上下文 - .filter

return mod.profileNameLabel.lowercased().contains(text.lowercased()) 

我使用SWIFT 3和8的Xcode我貼我的代碼行下面:

var intialProfiles = NSMutablearray() 

var profiles = NSMutablearray() 

func filterTableView(ind:Int,text:String) { 
     switch ind { 

     case selectedScope.Name.rawValue: 

      //fix of not searching when backspacing 
      profiles = intialProfiles.filter(using: { (mod) -> Bool in 
      return mod.profileNameLabel.lowered().contains(text.lowercased()) 
      }) 
      self.searchTableView.reloadData() 
+0

'NSMutableArray'斯威夫特是可怕的。使用本地Swift'Array'。 – vadian

回答

0

NSMutableArray.filter接受NSPredicate作爲參數,而不是(Profile) -> Bool關閉要傳遞的類型

我建議你使用,而不是NSMutableArray迅速陣列。

我假設該數組包含Profile對象。如果沒有,只需用下面的代碼中的Profile這個詞替換它所包含的任何類型。

var intialProfiles = [Profile]() 

var profiles = [Profile]() 

func filterTableView(ind:Int,text:String) { 
     switch ind { 

     case selectedScope.Name.rawValue: 

      //fix of not searching when backspacing 
      profiles = intialProfiles.filter { $0.profileNameLabel.lowered().contains(text.lowercased()) } 
      self.searchTableView.reloadData() 

如果你堅持要用NSMutableArray,這裏是你需要使用謂詞:

initialProfiles.filter(using: NSPredicate(format: 
    "profileNameLabel contains[c] %@", "Hello")) 
+0

感謝您的回覆。我嘗試了你推薦的謂詞代碼,它刪除了錯誤,但是當我運行它時,代碼沒有按照假設的方式運行,即searchBar沒有過濾數組。你知道爲什麼它沒有按照它應該的方式運行?另外我忘了提及我的數組是由從firebase檢索的數據組成的,因此我使用NSMutableArray的原因。 –

+0

你可以很容易地將一個'NSMutableArray'轉換成一個swift數組。通過查看您提供的代碼,我無法找到無法正常工作的原因。我需要更多的代碼來弄清楚。也許你應該檢查調用'filterTableView'的方法。表視圖數據源方法也可能是錯誤的地方。同時檢查'filter'位是否被執行。 @ j.iese – Sweeper

+0

好吧,所以我再次檢查過濾器位,它實際上根本不執行。我把一個打印語句放入並且只打印該文件正在執行。的代碼如下FUNC filterTableView(IND:中等,文本:字符串){ 開關IND { 情況selectedScope.Name.rawValue: profiles.filter(使用:NSPredicate(格式: 「profileNameLabel包含並[c]% @」, 「你好」)) self.searchTableView.reloadData() 打印( 「搜索」) –

相關問題