制作一串字符串

时间:2014-03-16 13:41:38

标签: ios arrays string nsstring nsarray

我有一堆在我的搜索谓词中使用的字符串 - 我想将它们放入一个数组中并搜索数组而不是单独搜索它们,因为单独搜索它们是 SLOW

现在这就是我的字符串设置方式

- (NSString *)searchableStringValue {
    NSCharacterSet *invalidSet = [NSCharacterSet characterSetWithCharactersInString:@"1234 "];
    NSString *searchString = [[pro componentsSeparatedByCharactersInSet:invalidSet] componentsJoinedByString:@""];
    return searchString;
}

- (NSString *)searchableStringValueTwo {
    NSCharacterSet *invalidSet = [NSCharacterSet characterSetWithCharactersInString:@"ȵ"];
    NSString *searchString = [[pro componentsSeparatedByCharactersInSet:invalidSet] componentsJoinedByString:@"n"];
    return searchString;
}

- (NSString *)searchableStringValueThree {
    NSCharacterSet *invalidSet = [NSCharacterSet characterSetWithCharactersInString:@"ŋ"];
    NSString *searchString = [[pro componentsSeparatedByCharactersInSet:invalidSet] componentsJoinedByString:@"ng"];
    return searchString;
}

- (NSString *)searchableStringValueFour {
    NSCharacterSet *invalidSet = [NSCharacterSet characterSetWithCharactersInString:@"Ẓ"];
    NSString *searchString = [[pro componentsSeparatedByCharactersInSet:invalidSet] componentsJoinedByString:@"r"];
    return searchString;
}

- (NSString *)searchableStringValueFive {
    NSCharacterSet *invalidSet = [NSCharacterSet characterSetWithCharactersInString:@"v"];
    NSString *searchString = [[pro componentsSeparatedByCharactersInSet:invalidSet] componentsJoinedByString:@"w"];
    return searchString;
}

- (NSString *)searchableStringValueSix {
    NSCharacterSet *invalidSet = [NSCharacterSet characterSetWithCharactersInString:@"1234"];
    NSString *searchString = [[pro componentsSeparatedByCharactersInSet:invalidSet] componentsJoinedByString:@""];
    return searchString;
}

- (NSString *)searchableStringValueSeven {
    NSCharacterSet *invalidSet = [NSCharacterSet characterSetWithCharactersInString:@" "];
    NSString *searchString = [[pro componentsSeparatedByCharactersInSet:invalidSet] componentsJoinedByString:@""];
    return searchString;
}

所以我想问一下将这些人放入阵列的最佳方法是什么? 这是最好的事情吗?!或者我只是愚蠢?有更好的方法吗?!

编辑:

搜索过滤器:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"(head beginswith[c] %@) OR (pro beginswith[c] %@ OR (searchableStringValue beginswith[c] %@) OR (searchableStringValueTwo beginswith[c] %@) OR (searchableStringValueThree beginswith[c] %@) OR (searchableStringValueFour beginswith[c] %@) OR (searchableStringValueFive beginswith[c] %@) OR (searchableStringValueSix beginswith[c] %@) OR (searchableStringValueSeven beginswith[c] %@))", searchText, searchText, searchText, searchText, searchText, searchText, searchText, searchText, searchText];
    searchResults = [cdh filteredArrayUsingPredicate:resultPredicate];
}

1 个答案:

答案 0 :(得分:0)

你不想进行这么多的比较。每个人花费一定的时间,每个人都基于计算值。 pro值在此期间是静态的,因此全部浪费了。

如果可以,请在pro上执行所有替换,并存储一个字符串以进行搜索。存放字符串,不要每次都应用所有替换。 pro更改时重新创建此搜索字符串。

如果您无法应用所有更改并生成一个搜索字符串,请至少存储搜索字符串,并仅在pro更改时重新创建。