简体   繁体   English

问:'[String]' 类型的值没有成员 'filtered'

[英]Q:Value of type '[String]' has no member 'filtered'

In my Code when I implementation my func : filterContentForSearchText(searchText:String, scope:String)在我的代码中,当我implementation我的funcfilterContentForSearchText(searchText:String, scope:String)

error comes:错误来了:

let resultPredicate:NSPredicate = NSPredicate.init(format: "name contains[c] \(searchText)", searchText)
resultDataSource = dataSource?.filtered(using: resultPredicate) as NSArray?  // the red error:Value of type '[String]' has no member 'filtered'

I post my code below:我在下面发布我的代码:

import UIKit

class StoreListViewController: UIViewController, UISearchBarDelegate,UISearchDisplayDelegate,UITableViewDelegate,UITableViewDataSource {

var dataSource:[String]? = nil
var resultDataSource:NSArray? = nil

override func viewDidLoad() {
    super.viewDidLoad()
    
    initData()
    initUI()
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

// MARK: - init
func initData(){

    self.searchDisplayController?.searchResultsDelegate = self
    self.searchDisplayController?.searchResultsDataSource = self
    self.searchDisplayController?.delegate = self
    
    dataSource = ["a","a","a","a" ]
}
func initUI() -> Void {
    
    
}


// MARK: - tableView delegate

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    if tableView == self.searchDisplayController?.searchResultsTableView {
        
        return (dataSource?.count)!
    }else {
    
        return (resultDataSource?.count)!
    }
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 50
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell_id = "StoreListTabCell"
    
    var cell:StoreListTabCell = tableView.dequeueReusableCell(withIdentifier: cell_id) as! StoreListTabCell
    

    cell.title_label.text = dataSource?[indexPath.row]
    
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    tableView.deselectRow(at: indexPath, animated: true)
}

// MARK: - filter delegate
func filterContentForSearchText(searchText:String, scope:String) -> Void {
    
//        NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
//        filteredist = [OldList filteredArrayUsingPredicate:resultPredicate];
    let resultPredicate:NSPredicate = NSPredicate.init(format: "name contains[c] \(searchText)", searchText)
    resultDataSource = dataSource?.filtered(using: resultPredicate) as NSArray?  // there is the error!!!
}

func searchDisplayController(_ controller: UISearchDisplayController, shouldReloadTableForSearch searchString: String?) -> Bool {
    
    //let scope_str = searchDisplayController?.searchBar.scopeButtonTitles.objectAtIndex(self.searchDisplayController.searchBar.selectedScopeButtonIndex)
    //[self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    
    var scopeTitles:Array = (searchDisplayController?.searchBar.scopeButtonTitles)!
    
    let selected_index = searchDisplayController?.searchBar.selectedScopeButtonIndex
    
    let scope_str = scopeTitles[selected_index!]
    
    
    filterContentForSearchText(searchText: searchString!, scope:scope_str)
    
    return true
}

}

And in my storyboard :在我的故事板中:

Because Swift array does not support NSArray's filterred with predicate, you have to use array.filter{ $0.contains(searchText) } (recommended) or array.filter{resultPredicate.evaluate(with:$0)} (to use with NSPredicate - totally not cool with swift) instead of the one you are using因为 Swift 数组不支持 NSArray's filterred with predicate,你必须使用array.filter{ $0.contains(searchText) } (推荐)或array.filter{resultPredicate.evaluate(with:$0)} (与 NSPredicate 一起使用 - 完全swift 不酷)而不是你正在使用的那个

Full:完整:

array.filter { (item) -> Bool in
            resultPredicate.evaluate(with: item)
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM