不能在集合SearchBarController中使用/ contains运算符

时间:2017-04-16 01:18:58

标签: ios swift uisearchbar

我正在尝试使用searchBar搜索数组,当我第一次点击searchBar时我得到了

不能在集合中使用/ contains运算符

我的代码如下...我是Swift中searchBars的新手,所以感谢任何帮助。

 struct Objects{
    var sectionName = String()
    var sectionObjects = [String]()
}
var objectsArray = [Objects]()
var filteredTableData = [String]()
lazy  var resultSearchController = UISearchController()


@IBOutlet weak var searchBar: UISearchBar!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    objectsArray = [Objects(sectionName: "A", sectionObjects: ["A&W Restaurant","Applebee's","Arby's","Arthur Treacher","Atlanta Bread Company","Au Bon Pain","Auntie Anne's"]),
                    Objects(sectionName: "B", sectionObjects: ["Backyard Burgers","Baja Fresh","Baskin-Robins","BD's Mongolian Grill","Beard Papa's","Big Apple Bagels","Biggby","Blackjack Pizza","Blimpie","Bob Evans Restaurant","Bojangles","Boloco","Boston Market Restaurant","Boston's Gourmet Pizza USA","Braum's","Breadsmith","Brown's Chicken & Pasta","Bruegger's","Bruster's","Buca Di Beppo","Buffalo Wild Wings","Burger King", "Burgerville"]),
                    Objects(sectionName: "C", sectionObjects: ["California Pizza Kitchen","Camille's Sidewalk Cafe","Captain D's Seafood","Caribou Coffee","Carl's Jr.","Carl's Jr. Green Burrito","Carraba's","Carvel","Casey's General Store","Cedarlane","Charley's Grilled Subs","Checkers/Rally's","Chevys Fresh Mex","Chick-fil-a","Chicken Express","Chicken Out Rotisserie","Chili's","Chipolte","Chuck E. Cheese","Church's Chicken", "Cici's Pizza","Cinnabon","Claim Jumper","Coffee Bean & Tea Leaf","Cold Stone Bakery Cafe","Cosi","Costco Food Court","Cousins Subs","Crispers","Culver's"]),
                    Objects(sectionName: "D", sectionObjects: ["D'Angelo","Dairy Queen","Daphne's Greek Cafe","Davanni's","Daylight Donuts","Del Taco","Denny's","Dickey's Barbecue Pit","Dippin' Dots","Domino's Pizza","Don Pablos","Donatos Pizza","DoubleDave's PizzaWorks","Dunkin' Donuts","Dunn Bros Coffee","Dutch Bros"]),
                    Objects(sectionName: "E", sectionObjects: ["East of Chicago","Eat 'n Park","Edo Japan","Einstein Bros Bagels","El Pollo Loco","Elephant Bar Restaurant","Erbert & Gerbert's","Extreme Pita"]),
                    Objects(sectionName: "F", sectionObjects: ["Famous Dave's BBQ Restaurant","Fatburger","Fazoli's Italian Food","Firehouse Subs","First Watch","Five Guys","Flame Broiler","Fox's Pizza Den","Freshens Smoothie Company","Friendly's","Frisch's Big Boy","Frullati"]),
                    Objects(sectionName: "G", sectionObjects: ["Gloria Jeans","Godfather's Pizza","Gold Star Chili","Golden Corral","Golden Spoon Frozen Yogurt","Good Times","Great American Bagel","Great Steak & Potato Company","Great Wraps"])
    ]

    self.resultSearchController = ({
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.sizeToFit()

        self.tableView.tableHeaderView = controller.searchBar

        return controller
    })()

    self.tableView.reloadData()

}
func updateSearchResults(for searchController: UISearchController) {


    filteredTableData.removeAll(keepingCapacity: false)

    let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
    let array = (objectsArray as NSArray).filtered(using: searchPredicate)
    filteredTableData = array as! [String]

    self.tableView.reloadData()
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! UITableViewCell!
    if (self.resultSearchController.isActive) {
        cell?.textLabel?.text = filteredTableData[indexPath.row]

        return cell!
    }
    else{
        cell?.textLabel?.text = objectsArray[indexPath.section].sectionObjects[indexPath.row]
        return cell!

    }
}

0 个答案:

没有答案