0

How could I enumerate an NSArray containing objects of multiple types, to get all the indexes where an NSString is found, then be able to refer to each index in order by saying something like...

NSString *firstOccurrence = [myArray objectAtIndex:firstOccurrence];
NSString *secondOccurrence = [myArray objectAtIndex:secondOccurrence];
NSString *thirdOccurrence = [myArray objectAtIndex:thirdOccurrence];

Thanks!

EDIT: How I'm using the code (Updated with @NJones example.)

I need the Integer value of the index where the strings are stored in the array, to update the NSUInteger property "wordDisplayed" with that value.

In my code here, I'm using a modified version of UIActionSheet to accept blocks: 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];
Jason
  • 171
  • 1
  • 11
  • Please have a look [here][1]. [1]: http://stackoverflow.com/questions/2802171/string-search-in-string-array-in-objective-c – SteAp Feb 21 '12 at 22:18
  • That does not help me. That assumes I know what the string text is. The strings will be dynamic, and there are more than just strings in the array. I think could do what I need using a for (id object in myArray) type enumeration, but am curious as to how I would do this using blocks. – Jason Feb 21 '12 at 22:25

2 Answers2

5

You can get an NSIndexSet of the location of objects that you define with indexesOfObjectsPassingTest:. Like so:

-(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];
}
NJones
  • 27,139
  • 8
  • 70
  • 88
  • Thank you!! Much better than what I came up with! ...See edited question. :) – Jason Feb 21 '12 at 22:47
  • Is there a way to refer to each index in the set, in order? I need individual access to each of the indexes stored in the set. Would I be better off using an array/nsnumber wrapping? – Jason Feb 21 '12 at 23:08
  • In your question you are assigning variables like `firstOccurrence` and so on to each. Is this a case where you will have a fixed number of strings, or you only care about the first so many? `NSIndexSet` is a bit limited for some cases, I'm just trying to figure out your precise use to see if it's a good fit. – NJones Feb 21 '12 at 23:24
  • 1
    I put an example of how to get an array from the results in my answer. Does this help you? – NJones Feb 21 '12 at 23:29
  • Your array example does help me use the strings as titles in my UIActionSheet(made to accept blocks) However, I do need the Integer values of the indexes @ which the NSStrings are located in the array. I've updated my question to show exactly what I'm doing with this. – Jason Feb 21 '12 at 23:45
  • Also, there are a fixed number of strings in the arrays I need to do this with. – Jason Feb 21 '12 at 23:55
  • 1
    `wordDisplayed = [arrayInLesson indexOfObject:[passingObjects objectAtIndex:0]];` for the first, and so on. Use autocomplete I typed this on iPad. – NJones Feb 22 '12 at 05:22
0

Well, the enumerateObjectsUsingBlock: method of NSArray will let you enumerate the objects in the array using a block (per your request).

To test for strings, you could simply do this:

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

And you could add each of those to a Dictionary with Object keys and Int values (or vice-versa).

Steve
  • 31,144
  • 19
  • 99
  • 122