“无法将 '(String) -> Bool' 类型的值转换为预期参数”
Posted
技术标签:
【中文标题】“无法将 \'(String) -> Bool\' 类型的值转换为预期参数”【英文标题】:"Cannot convert value of type '(String) -> Bool' to expected argument "“无法将 '(String) -> Bool' 类型的值转换为预期参数” 【发布时间】:2017-03-18 20:40:40 【问题描述】:我在使用 IndexPath.row 数据为我的 TableView
实现 SearchBar 时遇到了一些问题
我不知道如何处理此错误消息:
无法将 (String) -> Bool
类型的值转换为预期的参数类型 (postStruct) -> Bool
我在func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
中收到此错误消息
这是我的代码
import UIKit
import Firebase
import FirebaseDatabase
struct postStruct
let username : String!
let school : String!
class TableViewProfileController: UITableViewController, UISearchBarDelegate
@IBOutlet weak var searchBar: UISearchBar!
var posts:[postStruct] = []
override func viewDidLoad()
super.viewDidLoad()
self.title = "Find friends"
searchBar.delegate = self
tableView.dataSource = self
//Hide backButton in Navigationbar
self.navigationItem.setHidesBackButton(true, animated: false)
// SearchController
searchBar.sizeToFit()
searchBar.searchBarStyle = .minimal
searchBar.placeholder = "Search here..."
searchBar.setShowsCancelButton(false, animated: true)
searchBar.keyboardAppearance = .default
searchBar.tintColor = UIColor(red: 38.0/255.0, green: 68.0/255.0, blue: 102.0/255.0, alpha: 1.0)
self.definesPresentationContext = true
self.tableView.reloadData()
// TableView Cell Content
let databaseRef = FIRDatabase.database().reference()
databaseRef.child("users").queryOrderedByKey().observe(.childAdded, with:
snapshot in
let snapshotValue = snapshot.value as? NSDictionary
let username = snapshotValue!["Username"] as! String
let school = snapshotValue!["School"] as! String
self.posts.insert(postStruct(username: username, school: school) , at: 0)
self.tableView.reloadData()
)
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
posts = searchText.isEmpty ? posts : posts.filter((dataString: String) -> Bool in
return dataString.range(of: searchText, options: .caseInsensitive) != nil
)
tableView.reloadData()
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar)
self.searchBar.showsCancelButton = true
func searchBarCancelButtonClicked(_ searchBar: UISearchBar)
searchBar.showsCancelButton = false
searchBar.text = ""
searchBar.resignFirstResponder()
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
return 2.0
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return self.posts.count
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
let label1 = cell?.viewWithTag(1) as! UILabel
label1.text = posts[indexPath.row].username
label1.font = UIFont(name: "Arial Rounded MT Bold", size: 16)
label1.textColor = UIColor.black
let label2 = cell?.viewWithTag(2) as! UILabel
label2.text = posts[indexPath.row].school
label2.font = UIFont(name: "Arial", size: 11)
label2.textColor = UIColor(red: 102.0/255.0, green: 102.0/255.0, blue: 102.0/255.0, alpha: 1.0)
cell?.imageView?.image = UIImage(named: "Userlogo")
return cell!
//Preparing Segue to show username in FindUserViewController
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
performSegue(withIdentifier: "findSegue", sender: self.posts[indexPath.row].username)
//Segue to show username in FindUserViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
if segue.identifier! == "findSegue"
let guest = segue.destination as! FindUserViewController
guest.pickedUser = sender as! String
let backItem = UIBarButtonItem()
backItem.title = "Back"
navigationItem.backBarButtonItem = backItem
【问题讨论】:
【参考方案1】:您将filter
函数应用于postStruct
数组,因此语法至少为
posts.filter( (post: postStruct) -> Bool in ... )
或更短
posts.filter post in ...
你可以搜索
posts = searchText.isEmpty ? posts : posts.filter post in
return post.username.range(of: searchText, options: .caseInsensitive) != nil
旁注:
请,请不要 - 永远不要 - 在结构中声明隐式未包装的可选成员。
要么它们确实是可选的,然后将它们声明为String?
,要么它们是非可选的,然后去掉感叹号。
【讨论】:
谢谢.. 错误消失了。现在我只需要弄清楚为什么不进行任何搜索以上是关于“无法将 '(String) -> Bool' 类型的值转换为预期参数”的主要内容,如果未能解决你的问题,请参考以下文章