1

While the "for in" loop is running, is it possible to look up the value of the next "string"?

Lets say you had an array called myArrayOfStrings that had the following values in it:

0 "Apple" 1 "Pear" 2 "Orange" 3 "Grape"

And you wanted to iterate over the array using a "for in" loop:

for (NSString* string in myArrayOfStrings) { NSLog(string); }

Is there anyway, inside of the "for in" loop to look of the value of the next string while it's running? Lets say it was currently looking at the 1 "Pear" string. Would there be anyway to lookup the value of the next string 2 "Orange"?

1
  • Yeah, if you want access to the actual array index it's best to use a for(int i = ... type loop, vs "for in". You can keep your own index with a "for in" loop, but you lose the "connection" that the explicit loop index gives you. Commented May 15, 2013 at 22:42

1 Answer 1

4

While this is not directly achievable using a foreach-style loop in objective c, there are a couple ways to accomplish the same task.

Please assume that

NSArray *myArrayOfStrings = @[@"Apple", @"Pear", @"Orange", @"Grape"];

Standard For Loop

for (int i = 0; i < [myArrayOfStrings count]; i++) {
    NSString *string = [myArrayOfStrings objectAtIndex:i];
    NSString *next_string = @"nothing";
    if (i + 1 < [myArrayOfStrings count]) { // Prevent exception on the final loop
        next_string = [myArrayOfStrings objectAtIndex:i + 1];
    }
    NSLog(@"%@ comes before %@", string, next_string);
}

Object Enumeration

[myArrayOfStrings enumerateObjectsUsingBlock:^(NSString *string, NSUInteger idx, BOOL *stop) {
    NSString *next_string = @"nothing";
    if (idx + 1 < [myArrayOfStrings count]) { // Prevent exception on the final loop
        next_string = [myArrayOfStrings objectAtIndex:idx + 1];
    }
    NSLog(@"%@ comes before %@", string, next_string);
}];

Both of these options output:

Apple comes before Pear
Pear comes before Orange
Orange comes before Grape
Grape comes before nothing
Sign up to request clarification or add additional context in comments.

7 Comments

I had a feeling that was the case. I just wanted to make sure there wasn't some feature of "for in" loops that I wasn't aware of. Thanks.
You will get an out of bounds exception when you reach the last object. Use i < [myArrayOfStrings count] - 1 instead.
@zsnow It will when you assign to next_string, which looks for objectAtIndex:i + 1.
You may also want to look at -[NSArray enumerateObjectsUsingBlock:], which loops with a block that's passed both an object and its index. That can save you the assignment to string in this example.
@zsnow Looks good, but there's actually nothing stopping you from changing id obj in the block's parameter list to NSString * string. That's what I usually do.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.