如何枚舉包含多個類型的對象的NSArray,以獲取找到NSString的所有索引,然後能夠引用每個索引順序說一些像...使用塊枚舉NSArray來查找並存儲數組中的所有NSString的所有索引的
NSString *firstOccurrence = [myArray objectAtIndex:firstOccurrence];
NSString *secondOccurrence = [myArray objectAtIndex:secondOccurrence];
NSString *thirdOccurrence = [myArray objectAtIndex:thirdOccurrence];
謝謝!
編輯:我如何使用的代碼(更新與@NJones例子)
我需要索引的整數值,其中的字符串存儲在陣列中,更新NSUInteger財產「wordDisplayed」與那個價值。
在這裏我的代碼,我使用UIActionSheet的修改版本,接受塊: https://github.com/zoul/Lambda-Alert
NSIndexSet *stringLocations = [arrayInLesson indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop){
return [(NSObject *)obj isKindOfClass:[NSString class]];
}];
NSArray *passingObjects = [arrayInLesson objectsAtIndexes:stringLocations];
sectionHeadersAct = [[LambdaSheet alloc] initWithTitle:@"Book 2 Lesson 1"];
[sectionHeadersAct addButtonWithTitle:@"D. E. F. & G. Teach New Letters" block:^{
//Do nothing yet
}];
[sectionHeadersAct addButtonWithTitle:[passingObjects objectAtIndex:0] block:^{
NSLog(@"First");
wordDisplayed = theIndexOfThisStringIn_arrayInLesson;
}];
[sectionHeadersAct addButtonWithTitle:[passingObjects objectAtIndex:1] block:^{
NSLog(@"Second");
wordDisplayed = theIndexOfThisStringIn_arrayInLesson;
}];
[sectionHeadersAct addButtonWithTitle:[passingObjects objectAtIndex:2] block:^{
NSLog(@"Third");
wordDisplayed = theIndexOfThisStringIn_arrayInLesson;
}];
[sectionHeadersAct addButtonWithTitle:[passingObjects objectAtIndex:3] block:^{
NSLog(@"Fourth");
wordDisplayed = theIndexOfThisStringIn_arrayInLesson;
}];
[sectionHeadersAct setDismissAction:^{
//Do nothing yet
}];
[sectionHeadersAct showInView:self.view];
請看看[這裏] [1]。 [1]:http://stackoverflow.com/questions/2802171/string-search-in-string-array-in-objective-c – SteAp 2012-02-21 22:18:30
這並不能幫助我。假設我知道字符串文本是什麼。這些字符串將是動態的,並且數組中不僅有字符串。我認爲可以使用for(id對象在myArray中)類型枚舉來完成我所需要的操作,但對於如何使用塊來完成此操作感到好奇。 – Jason 2012-02-21 22:25:38