IOS Parse如何在字符串数组中搜索子字符串?

时间:2014-11-08 09:37:06

标签: ios objective-c parse-platform pfquery

我使用解析数据库来存储一些记录。我正在存储一个字符串数组,我希望能够在这个数组中搜索子字符串。这是一个例子:

数组A:

["carbrown","blue","house","coldturkey"]

数组B:

["racecar","green","walking"]

数组C:

["greenturkey","users","published","ramp"]

我希望能够搜索像car这样的子字符串,并将数组A和B作为匹配结果,或者搜索turkey会得到与数组A和C匹配的结果,或者{{ 1}}给我数组B和C,等等..

我知道对于字符串,您可以在解析中使用它:

green

这可能是数组,也许是正则表达式?

1 个答案:

答案 0 :(得分:2)

您可以将NSPredicate用于此目的。以下是一个例子。

NSArray *a = @[@"carbrown",@"blue",@"house",@"coldturkey"];
NSArray *b = @[@"racecar",@"green",@"walking"];
NSArray *c = @[@"greenturkey",@"users",@"published",@"ramp"];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", @"car"];
NSArray *filteredArrayA = [a filteredArrayUsingPredicate:predicate];
NSArray *filteredArrayB = [b filteredArrayUsingPredicate:predicate];
NSArray *filteredArrayC = [c filteredArrayUsingPredicate:predicate];

if ([filteredArrayA count]) {
    NSLog(@"A has car in it");
}