Query Parse containsString make不敏感

时间:2014-05-07 08:13:26

标签: ios parse-platform uisearchbar

我想知道是否有人可以提供帮助。我正在尝试使用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];
}

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:29)

使用正则表达式(?i)代替!赠送金额为https://stackoverflow.com/a/9655203/2398886https://stackoverflow.com/a/9655186/2398886http://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")

参考:https://docs.parseplatform.org/ios/guide/#queries