我試圖找到一個特定的字符串開始NSString的數組中的所有對象,並返回一個包含所有這些對象的小陣列。例如,你可以有一個數組對象:NSArray的一個數組包含對象(S)開始用繩子
'Cat',
'Dog',
'Dolphin',
'Whale'
而且,當搜索字符串是do
,一個新的NSArray只與Dog
和Dolphin
對象返回。使用謂詞上
-(NSArray *)entriesInArray:(NSArray *)wordsArray whichStartWithString:(NSString *)startString {
startString = [startString lowercaseString]; // get rid of this for case-sensitive (& the lowercaseString bit in the loop too)
NSMutableArray *resultsArray = [NSMutableArray array];
for (NSString *thisString in wordsArray) {
if ([[thisString lowercaseString] hasPrefix:startString]) [resultsArray addObject:thisString];
}
return resultsArray;
}
這是如此嚇壞時髦。我不得不添加「SELF BEGINSWITH [cd]%@」,而不是僅僅使用普通的%@。 – 2014-07-04 15:10:03