简体   繁体   English

二进制运算符'=='不能应用于'NSObject'和'String'类型的操作数

[英]Binary operator '==' cannot be applied to operands of type 'NSObject' and 'String'

I am trying to search users on a UITableView, through a UISearchBar. 我正在尝试通过UISearchBar在UITableView上搜索用户。 The thing is that an error keeps appearing when I try to. 事实是,当我尝试这样做时,错误始终出现。

Here's my code: 这是我的代码:

 var isSearching = false
 func searchBar(_ searchBar: UISearchBar, textDidChange searchText:String) {
     if searchBar.text == nil || searchBar.text == "" {
        isSearching = false
        view.endEditing(true)
        searchTableView.reloadData()
    } else {
        isSearching = true
        filteredUsers = users.filter({$0 == searchBar.text!})  // error appears here

    }

    searchTableView.reloadData()
}

I am trying to search for the users in my app, through Firebase fetch. 我正在尝试通过Firebase提取在我的应用程序中搜索用户。 I've already fetched the users onto a UITableView, the thing is I cant search for a specific one. 我已经将用户提取到UITableView上,问题是我无法搜索特定的用户。 Please help! 请帮忙! I've tried everything. 我已经尝试了一切。

A type NSObject can't compared with a type of String , you need NSObject类型不能与String类型进行比较,您需要

filteredUsers = users.filter({$0.someProperty == searchBar.text!}) 

where someProperty is a property inside the class 其中someProperty是类内的属性

暂无
暂无

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

相关问题 二元运算符“+”不能应用于“_”和“String”类型的操作数 - Binary operator '+' cannot be applied to operands of type '_' and 'String' 二元运算符“+”不能应用于“字符串”和“字符串?”类型的操作数 - Binary operator '+' cannot be applied to operands of type 'String' and 'String?' 二进制运算符“ +”不能应用于类型为“字符串”和“字符串感叹号”的操作数 - Binary operator '+' cannot be applied to operands of type 'String' and 'String exclamationmark 二元运算符“==”不能应用于“[String]”和“String”类型的操作数 - Binary operator '==' cannot be applied to operands of type '[String]' and 'String 二进制运算符'+'不能应用于'String'和'()-> String'类型的操作数 - Binary operator '+' cannot be applied to operands of type 'String' and '() -> String' 二进制运算符'〜='不能应用于'String'和'String?'类型的操作数 - Binary operator '~=' cannot be applied to operands of type 'String' and 'String?' 二进制运算符-不能应用于字符串类型的操作数! 和诠释 - Binary operator - cannot be applied to operands of type String! and Int 二进制运算符'=='不能应用于'Any?'类型的操作数 和'String'Swift iOS - Binary operator '==' cannot be applied to operands of type 'Any?' and 'String' Swift iOS 二进制运算符'=='不能应用于类型'UIImage?'的操作数 和“字符串”-Swift 4 - Binary operator '==' cannot be applied to operands of type 'UIImage?' and 'String' - Swift 4 Swift-二进制运算符'> ='不能应用于类型为'String'和'Int'的操作数 - Swift - Binary operator '>=' cannot be applied to operands of type 'String' and 'Int'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM