Created
July 23, 2012 13:09
-
-
Save adib/3163537 to your computer and use it in GitHub Desktop.
Sparse Array - FoundationAdditions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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