Skip to content

Instantly share code, notes, and snippets.

@adib
Created July 23, 2012 13:09
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 adib/3163537 to your computer and use it in GitHub Desktop.
Save adib/3163537 to your computer and use it in GitHub Desktop.
Sparse Array - FoundationAdditions
@implementation NSArray (FoundationAdditions)
-(id) objectAtCheckedIndex:(NSUInteger) index {
if(index >= self.count) {
return nil;
} else {
id result = [self objectAtIndex:index];
return result == [NSNull null] ? nil : result;
}
}
@end
@implementation NSMutableArray (FoundationAdditions)
-(void) setObject:(id) object atCheckedIndex:(NSUInteger) index {
NSNull* null = [NSNull null];
if (!object) {
object = null;
}
NSUInteger count = self.count;
if (index < count) {
[self replaceObjectAtIndex:index withObject:object];
} else {
if (index > count) {
NSUInteger delta = index - count;
for (NSUInteger i=0; i<delta;i++) {
[self addObject:null];
}
}
[self addObject:object];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment