Jas*_*son 0 iphone objective-c ios
我如何枚举包含多种类型对象的NSArray,以获取找到NSString的所有索引,然后能够通过说出类似的内容来按顺序引用每个索引...
NSString *firstOccurrence = [myArray objectAtIndex:firstOccurrence];
NSString *secondOccurrence = [myArray objectAtIndex:secondOccurrence];
NSString *thirdOccurrence = [myArray objectAtIndex:thirdOccurrence];
Run Code Online (Sandbox Code Playgroud)
谢谢!
编辑:我如何使用代码(更新@NJones示例.)
我需要字符串存储在数组中的索引的Integer值,以使用该值更新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];
Run Code Online (Sandbox Code Playgroud)
您可以获取NSIndexSet您定义的对象的位置indexesOfObjectsPassingTest:.像这样:
-(void)findStrings{
NSArray *randomObjects = [NSArray arrayWithObjects:[NSNull null], @"String", [NSNull null], @"String", [NSNull null], nil];
NSIndexSet *stringLocations = [randomObjects indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop){
return [(NSObject *)obj isKindOfClass:[NSString class]];
}];
NSLog(@"strings %@",stringLocations);
// You can get an array of just the passing objects like so:
NSArray *passingObjects = [randomObjects objectsAtIndexes:stringLocations];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1934 次 |
| 最近记录: |