【问题标题】:IOS Parse How to search an array of strings for a substring?IOS Parse 如何在字符串数组中搜索子字符串?
【发布时间】:2014-11-08 09:37:06
【问题描述】:

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

数组 A:

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

数组 B:

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

数组 C:

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

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

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

- (void)whereKey:(NSString *)key containsString:(NSString *)substring

这对数组是否可行,也许是正则表达式?

【问题讨论】:

  • 嗨@SuperKevin你找到这个问题的解决方案了吗?

标签: ios objective-c parse-platform pfquery


【解决方案1】:

您可以为此目的使用 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");
}

【讨论】:

  • 谢谢,但是这些数组在解析系统中。我在问如何从解析中提取这些匹配值,我的内存中没有这些数组。
  • 对不起,我不知道。不熟悉 Parse。
猜你喜欢
  • 2022-01-23
  • 2011-07-04
  • 2011-07-22
  • 1970-01-01
  • 2023-04-05
  • 2013-01-09
  • 1970-01-01
相关资源
最近更新 更多