我想知道有人能帮忙吗?我正在尝试使用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]; }
任何帮助将不胜感激.
解决方法
使用正则表达式(?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"];