ios – 查询解析containsString不敏感

前端之家收集整理的这篇文章主要介绍了ios – 查询解析containsString不敏感前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想知道有人能帮忙吗?我正在尝试使用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/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"];
原文链接:https://www.f2er.com/iOS/336232.html

猜你在找的iOS相关文章