gpt4 book ai didi

swift - 使用 Swift 2 进行不区分大小写(不区分大小写)搜索

转载 作者:行者123 更新时间:2023-11-30 10:08:36 25 4
gpt4 key购买 nike

我在 Parse 中有一个名为“Friends”的类(class)。我添加了一个搜索栏(没有搜索显示 Controller )。我希望能够通过搜索“john doe”来搜索“John Doe”。我能够做到这一点的唯一方法是在 Parse 中将 "first_name""last_name" 下的“ friend ”全部小写。然后我将 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()
}


}

最佳答案

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

let range = temp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
if range.location != NSNotFound {
print("match")
}

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

关于swift - 使用 Swift 2 进行不区分大小写(不区分大小写)搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34573217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com