Skip to content

Instantly share code, notes, and snippets.

@Abizern
Created October 16, 2010 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Abizern/629755 to your computer and use it in GitHub Desktop.
Save Abizern/629755 to your computer and use it in GitHub Desktop.
Perform a block on a sequential number of objects in an array forwards or backwards.
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Setup a test array
NSArray *test = [NSArray arrayWithObjects:@"Object 1", @"Object 2", @"Object 3", @"Object 4", @"Object 5", nil];
// Test 1 - run through the first 3 items
NSLog(@"<<<<<<Test 1>>>>>>");
[test dsl_enumerateUpTo:3 inDirection:DSLEnumerationForward usingBlock:^(id theObject, NSUInteger idx, BOOL *stop) {
NSLog(@"%@", theObject);
}];
// Test 2 - run through the first 3 items backwards
NSLog(@"<<<<<<Test 2>>>>>>");
[test dsl_enumerateUpTo:3 inDirection:DSLEnumerationReverse usingBlock:^(id theObject, NSUInteger idx, BOOL *stop) {
NSLog(@"%@", theObject);
}];
// Test 3 - run through the first 3 items forwards, but stop on "Object 2"
NSLog(@"<<<<<<Test 3>>>>>>");
[test dsl_enumerateUpTo:3 inDirection:DSLEnumerationForward usingBlock:^(id theObject, NSUInteger idx, BOOL *stop) {
NSString *testString = @"Object 2";
NSLog(@"%@", theObject);
if ([theObject isEqualToString:testString]) {
NSLog(@"Test string found: %@", testString);
*stop = YES;
}
}];
// Test 4 - run through the first 3 items backwards, but stop on "Object 4"
NSLog(@"<<<<<<Test 4>>>>>>");
[test dsl_enumerateUpTo:3 inDirection:DSLEnumerationReverse usingBlock:^(id theObject, NSUInteger idx, BOOL *stop) {
NSString *testString = @"Object 4";
NSLog(@"%@", theObject);
if ([theObject isEqualToString:testString]) {
NSLog(@"Test string found: %@", testString);
*stop = YES;
}
}];
[pool drain];
return 0;
}
//
// NSArray+DSLFirstItems.h
//
// Created by Pete Callaway on 15/10/2010.
// Copyright 2010 Dative Studios. All rights reserved.
// Modified by Abizer Nasir
//
#import <Foundation/Foundation.h>
enum _DSLEnumerationOption {
DSLEnumerationForward = 0,
DSLEnumerationReverse = 1
};
typedef NSUInteger DSLEnumerationOption;
@interface NSArray (DSLFirstItems)
- (void)dsl_enumerateUpTo:(NSUInteger)numberOfItemsToEnumerate inDirection:(DSLEnumerationOption)direction usingBlock:(void (^)(id object, NSUInteger idx, BOOL *stop))block;
@end
//
// NSArray+DSLFirstItems.m
//
// Created by Pete Callaway on 15/10/2010.
// Copyright 2010 Dative Studios. All rights reserved.
// Modified by Abizer Nasir
//
#import "NSArray+DSLFirstItems.h"
@implementation NSArray (DSLFirstItems)
- (void)dsl_enumerateUpTo:(NSUInteger)numberOfItemsToEnumerate inDirection:(DSLEnumerationOption)direction usingBlock:(void (^)(id object, NSUInteger idx, BOOL *stop))block {
NSInteger count = 0;
BOOL stop = NO;
id <NSFastEnumeration> iterable;
if (direction == DSLEnumerationForward) {
iterable = self;
} else {
iterable = [self reverseObjectEnumerator];
}
for (id object in iterable) {
block(object, count, &stop);
if (stop) return;
count++;
if (count >= numberOfItemsToEnumerate) return;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment