案例不敏感(不区分大小写)使用Swift 2搜索

时间:2016-01-03 04:04:04

标签: swift parse-platform

我在Parse有一个叫做#34; Friends"的课程。我添加了一个搜索栏(没有搜索显示控制器)。我希望能够搜索" John Doe"通过搜索" john doe"。我能做到的唯一方法就是让朋友们成为朋友。在Parse全部小写的"first_name""last_name"下。然后我将searchBar.text转换为lowerCaseString,它可以工作。我似乎无法将"first_name""last_name"转换为lowerCaseString
有任何想法吗?谢谢!

class ViewController: UIViewController, UITableViewDataSource,UITableViewDelegate, UISearchBarDelegate {
@IBOutlet weak var myTableView: UITableView!
@IBOutlet weak var mySearchBar: UISearchBar!


var searchResults = [String]()

override func viewDidLoad() {
    super.viewDidLoad()



}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

func searchBarSearchButtonClicked(searchBar: UISearchBar)
{
    searchBar.resignFirstResponder()
    print("Search word = \(searchBar.text!)")


    let firstNameQuery = PFQuery(className:"Friends")
    firstNameQuery.whereKey("first_name".lowercaseString, matchesRegex: searchBar.text!.lowercaseString)



    let lastNameQuery = PFQuery(className:"Friends")
    lastNameQuery.whereKey("last_name".lowercaseString, matchesRegex: searchBar.text!.lowercaseString)

    // lastNameQuery.whereKey("last_name", matchesRegex: "(?i)\(searchBar.text)") <- Instead of firstNameQuery.whereKey("first_name", containsString: searchBar.text)






    let query = PFQuery.orQueryWithSubqueries([firstNameQuery, lastNameQuery])

    query.findObjectsInBackgroundWithBlock {
        (results: [AnyObject]?, error: NSError?) -> Void in

        if error != nil {
            let myAlert = UIAlertController(title:"Alert", message:error?.localizedDescription, preferredStyle:UIAlertControllerStyle.Alert)

            let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)

            myAlert.addAction(okAction)

            self.presentViewController(myAlert, animated: true, completion: nil)

            return
        }

        if let objects = results as? [PFObject] {

            self.searchResults.removeAll(keepCapacity: false)

            for object in objects {
                let firstName = object.objectForKey("first_name") as! String
                let lastName = object.objectForKey("last_name") as! String
                let fullName = firstName + " " + lastName

                self.searchResults.append(fullName)
            }

            dispatch_async(dispatch_get_main_queue()) {
                self.myTableView.reloadData()
                self.mySearchBar.resignFirstResponder()

            }



        }




    }

}




func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return searchResults.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let myCell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) 

    myCell.textLabel?.text = searchResults[indexPath.row]

    return myCell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
    mySearchBar.resignFirstResponder()
}

func searchBarCancelButtonClicked(searchBar: UISearchBar)
{
    mySearchBar.resignFirstResponder()
    mySearchBar.text = ""
}

@IBAction func refreshButtonTapped(sender: AnyObject) {
    mySearchBar.resignFirstResponder()
    mySearchBar.text = ""
    self.searchResults.removeAll(keepCapacity: false)
    self.myTableView.reloadData()
}


}

2 个答案:

答案 0 :(得分:1)

使用NSStringCompareOption进行不区分大小写的搜索。如果使用此函数,则结果将显示包含子字符串的所有字符串。

customer_id

如果您只想要以特定搜索字符串开头的结果。只需检查范围是否从字符串的第一个字符开始。

答案 1 :(得分:0)

将两个文本都转换为小写(搜索文本和数组字符串)

   let text = searchText.lowercased()
        filtered = data.filter({

            let t = $0.dr_name.lowercased().contains(text)
           
            if t{
                return true
            }else{
                 return false
            }
           
            
        })