【问题标题】:Enumerate NSArray using blocks to find and store all indexe's of all NSStrings in array使用块枚举 NSArray 以在数组中查找和存储所有 NSStrings 的所有索引
【发布时间】: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


【解决方案1】:

您可以获得使用indexesOfObjectsPassingTest: 定义的对象位置的NSIndexSet。像这样:

-(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];
}

【讨论】:

  • 谢谢!!比我想出来的好多了! ...见编辑的问题。 :)
  • 有没有办法按顺序引用集合中的每个索引?我需要单独访问存储在集合中的每个索引。使用数组/nsnumber 包装会更好吗?
  • 在您的问题中,您正在为每个变量分配 firstOccurrence 等变量。这是您将拥有固定数量的字符串的情况,还是您只关心前这么多? NSIndexSet 在某些情况下有点受限,我只是想弄清楚你的确切用途,看看它是否合适。
  • 我在答案中放了一个如何从结果中获取数组的示例。这对你有帮助吗?
  • wordDisplayed = [arrayInLesson indexOfObject:[passingObjects objectAtIndex:0]]; 第一个,依此类推。使用自动完成我在 iPad 上输入的。
【解决方案2】:

好吧,NSArrayenumerateObjectsUsingBlock: 方法将允许您使用块(根据您的请求)枚举数组中的对象。

要测试字符串,您可以简单地这样做:

if ([itemToTest isKindOfClass:[NSString class]]) ...

您可以将它们中的每一个添加到带有 Object 键和 Int 值的字典中(反之亦然)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-12
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-03
    相关资源
    最近更新 更多