搜索栏textDidChange错误

时间:2017-05-16 00:08:39

标签: swift uitableview search filter uisearchbar

我正在尝试为tableView实现一个搜索栏,我收到错误“...二进制运算符'=='不能应用于textDidChange方法中'Place'和'String'类型的操作数。 tableView是从Firebase数据库“placeList”数组填充的。不确定错误源的来源。在此先感谢您的帮助!

lazy var searchBar:UISearchBar = UISearchBar()

var placeList = [Place]()
var placesDictionary = [String: Place]()

var isSearching = false
var filteredData = [Place]()

override func viewDidLoad() {
    super.viewDidLoad()

    searchBar.searchBarStyle = UISearchBarStyle.prominent
    searchBar.placeholder = " Search Places..."
    searchBar.sizeToFit()
    searchBar.isTranslucent = false
    searchBar.backgroundImage = UIImage()
    searchBar.delegate = self
    searchBar.returnKeyType = UIReturnKeyType.done
    navigationItem.titleView = searchBar

    tableView.allowsMultipleSelectionDuringEditing = true

}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellId)

    if isSearching {
        cell.textLabel?.text = filteredData[indexPath.row].place
    } else {

    cell.textLabel?.text = placeList[indexPath.row].place

    }
    return cell
}

func searchBar(_ searchBar:UISearchBar,textDidChange searchText:String){

    if searchBar.text == nil || searchBar.text == "" {
        isSearching = false
        view.endEditing(true)
        tableView.reloadData()
    } else {
        isSearching = true
        // error in below line of code...
        filteredData = placeList.filter({$0.place == searchBar.text})
        tableView.reloadData()
    }

}

1 个答案:

答案 0 :(得分:1)

您的属性m_NamePlotC[m_NameList.at(i)]->setPen(QPen(coloursList[i], 1, Qt::DashLine)); placeList个对象的数组。当您调用数组上的Place函数(filter)时,您所说的是“过滤placeList,其中Place对象等于searchBar.text”。地方对象不是placeList.filter({$0 == searchBar.text!}),您无法比较两种不同的类型。我不熟悉您的数据模型或您的String类,但是您可能在Place类中使用某种类型的String属性来比较它?例如,假设Place有一个名为Place的属性为String的属性,您可以按比例过滤:id - 注意添加的filteredData = placeList.filter({$0.id == searchBar.text!})

您只能将字符串与字符串进行比较

相关问题