Skip to content

Instantly share code, notes, and snippets.

@AlanQuatermain
Created May 18, 2012 13:39
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 AlanQuatermain/2725326 to your computer and use it in GitHub Desktop.
Save AlanQuatermain/2725326 to your computer and use it in GitHub Desktop.
A non-zero-based array. Guess what base it uses-- it could be anything!
@interface AQNonZeroBasedArray : NSArray
@end
@implementation AQNonZeroBasedArray
{
NSArray * _realArray;
NSUInteger _base;
}
// initialize it using AQNonZeroBasedArray * array = @[obj1, obj2, obj3];
- (id) initWithObjects: (id __unsafe_unretained []) objects count: (NSUInteger) count
{
self = [super init];
if ( self == nil )
return ( nil );
_realArray = [[NSArray alloc] initWithObjects: objects count: count];
_base = arc4random() % 100; // yeah, I'm not TOTALLY evil...
return ( self );
}
- (NSUInteger) count
{
return ( [_realArray count] );
}
- (id) objectAtIndex: (NSUInteger) index
{
index -= _base;
if ( NSLocationInRange(index, NSMakeRange(0, [_realArray count])) == NO )
[NSException raise: @"AQNonZeroBaseSurpriseException" format: @"HAHA LOLWUT!?"];
return ( [_realArray objectAtIndex: index] );
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment