使用未解析的标识符'范围'为什么这个错误会出现在下面的代码中

时间:2015-09-04 10:16:51

标签: ios xcode swift

这是一个搜索糖果的程序 但Xcode将显示未解决的范围标识符错误

import UIKit

class SearchTableViewController: UITableViewController, UISearchBarDelegate, UISearchDisplayDelegate {

    var searches = [Search]()

    var filteredSearches = [Search]()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.searches = [Search(category:"Chocolate", name:"chocolate Bar"),
            Search(category:"Chocolate", name:"chocolate Chip"),
            Search(category:"Chocolate", name:"dark chocolate"),
            Search(category:"Hard", name:"lollipop"),
           Search(category:"Hard", name:"candy cane"),
            Search(category:"Hard", name:"jaw breaker"),
            Search(category:"Other", name:"caramel"),
            Search(category:"Other", name:"sour chew"),
            Search(category:"Other", name:"gummi bear")]

        // Reload the table
        self.tableView.reloadData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func filterContentForSearchText(searchText: String) {
        // Filter the array using the filter method

        self.filteredSearches = self.searches.filter({( search: Search) -> Bool in

            let categoryMatch = (scope == "All") || (search.category == scope)

            let stringMatch = search.name.rangeOfString(searchText)

            return categoryMatch && (stringMatch != nil)
            //return true
        })
    }
    func searchDisplayController(controller:UISearchDisplayController, shouldReloadTableForSearchString searchString: String!) -> Bool {
        self.filterContentForSearchText(searchString)
        return true
    }

    func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchScope searchOption: Int) -> Bool
    {
        self.filterContentForSearchText(self.searchDisplayController!.searchBar.text)
        return true
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView == self.searchDisplayController!.searchResultsTableView {
            return self.filteredSearches.count
        } else {
            return self.searches.count
        }
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {

        let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell

        var search : Search
        // Check to see whether the normal table or search results table is being displayed and set the Candy object from the appropriate array
        if tableView == self.searchDisplayController!.searchResultsTableView {
            search = filteredSearches[indexPath.row]
        } else {
            search = searches[indexPath.row]
        }

        // Configure the cell
        cell.textLabel!.text = search.name
        cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator

        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.performSegueWithIdentifier("searchDetail", sender: tableView)
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        if segue.identifier == "searchDetail"

        {
            let candyDetailViewController = segue.destinationViewController as! UIViewController
            if sender as! UITableView == self.searchDisplayController!.searchResultsTableView
            {
                let indexPath = self.searchDisplayController!.searchResultsTableView.indexPathForSelectedRow()!
                let destinationTitle = self.filteredSearches[indexPath.row].name
                candyDetailViewController.title = destinationTitle
            } else {
                let indexPath = self.tableView.indexPathForSelectedRow()!
                let destinationTitle = self.searches[indexPath.row].name
                candyDetailViewController.title = destinationTitle
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

方法实现不正确,这是正确的方法:

func filterContentForSearchText(searchText: String, scope: String = "All") {
        self.filteredCandies = self.candies.filter({( candy : Candy) -> Bool in
            var categoryMatch = (scope == "All") || (candy.category == scope)
            var stringMatch = candy.name.rangeOfString(searchText)
            return categoryMatch && (stringMatch != nil)
            })
    }

您忘记在方法中声明变量'scope';)