Example. I've got an array with 15 objects. I want to start enumerating from a given index. Say start at index 5 and then the index above, the index under, above, under etc... I don't want it to wrap around, but rather stop and continue in unexplored direction.
So the order of indexes in my example would be. 5, 6, 4, 7, 3, 8, 2, 9, 1, 10, 0, 11, 12, 13, 14
How can this be done?
Here's a more compact implementation that doesn't require creating subarrays:
@implementation NSArray (Extensions)
- (void)enumerateFromIndex:(NSUInteger)index goBothWaysUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block
{
BOOL stop = NO;
for (NSUInteger i = 0; i < self.count && !stop; i++) {
if (index + i < self.count) {
block([self objectAtIndex:index + i], index + i, &stop);
}
if (i != 0 && !stop && i <= index) {
block([self objectAtIndex:index - i], index - i, &stop);
}
}
}
@end
-(void)enumerateArray:(NSArray *)array inBothDirectionsFromIndex:(int)startIndex
{
for (int i=0; i<array.count; i++)
{
int index = startIndex;
int indexAfter = startIndex + round(i/2.f) + (i%2 ? 0 : 1);
int indexBefore = startIndex - round(i/2.f);
if ((i%2 && indexAfter < array.count) || indexBefore < 0)
{
index = indexAfter;
if (indexBefore < 0)
index -= indexBefore + 1;
}
else if ((i > 0 && indexBefore > -1) || indexAfter > array.count-1)
{
index = indexBefore;
if (indexAfter > array.count-1)
index -= indexAfter - array.count;;
}
id item = [array objectAtIndex:index];
//Do what you want with the item here
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With