Watch next object in NSEnumerator

840 views Asked by At

I have a NSEnumerator object with the text lines retrieved from a NSTextView, parsed with:

NSEnumerator *myLines = [[allTheText componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] objectEnumerator];

I'm processing the lines inside a for cycle like this:

for (NSString *current_line in myLines)
{
     //doing something

}

This works fine, but now I need to watch the next object while processing the current one. What would be the best way to do this without disrupting the current cycle? If I do something like this (inside the current cycle), it won't work:

//inside the previous cycle                    
NSString *next_line;

if (next_line = [myLines nextObject])
{
   //process next line
}
1

There are 1 answers

6
Eimantas On

You can use standard for loop:

for(uint i = 0; i < [myLines count]; i++) {
    ...
}

You'd be able to get previous and next lines be using iteration index:

[myLines objectAtIndex:(i-1)]; // previous
[myLines objectAtIndex:(i+1)]; // next

Make sure to check for their existence first.

update

NSArray *lineArray = [allTheText componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];

Use array above in the for loop.