我想知道是否有人可以提供帮助。我正在尝试使用SearchBar
在Parse类中搜索一个术语。但containsString
区分大小写,我希望它不区分大小写。请参阅下面的代码;
-(void)filterResults:(NSString *)searchTerm {
[self.searchResults removeAllObjects];
PFQuery *query = [PFQuery queryWithClassName: @"Firefacts"];
[query whereKeyExists:@"Number"];
[query whereKey:@"Number" containsString:searchTerm];
NSArray *results = [query findObjects];
[self.searchResults addObjectsFromArray:results];
}
非常感谢任何帮助。
答案 0 :(得分:29)
使用正则表达式(?i)
代替!赠送金额为https://stackoverflow.com/a/9655203/2398886,https://stackoverflow.com/a/9655186/2398886和http://www.regular-expressions.info/modifiers.html
所以替换
[query whereKey:@"Number" containsString:searchTerm];
与
[query whereKey:@"Number" matchesRegex:[NSString stringWithFormat:@"(?i)%@",searchTerm]];
或者您也可以使用
[query whereKey:@"Number" matchesRegex:searchTerm modifiers:@"i"];
您的最终代码应为:
PFQuery *query = [PFQuery queryWithClassName: @"Firefacts"];
[query whereKeyExists:@"Number"];
[query whereKey:@"Number" matchesRegex:searchTerm modifiers:@"i"];
答案 1 :(得分:0)
对于Swift,请使用以下代码:
query.whereKey("Number", matchesRegex: searchTerm, modifiers: "i")