【发布时间】:2012-02-21 22:14:36
【问题描述】:
我如何枚举一个包含多种类型对象的 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]:stackoverflow.com/questions/2802171/…
-
这对我没有帮助。假设我知道字符串文本是什么。字符串将是动态的,并且数组中不仅仅是字符串。我认为可以使用 for (id object in myArray) 类型枚举来做我需要的事情,但我很好奇我将如何使用块来做到这一点。
标签: iphone objective-c ios