“无法将类型'(String) - > Bool'的值转换为预期参数”

时间:2017-03-18 20:40:41

标签: ios swift uitableview uisearchbar

我在使用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 个答案:

答案 0 :(得分: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?,或者它们是非可选的,然后剪切感叹号。