Just throw the punch to the left:
The idea is for the key-value encoding mechanism to create an array for you using indexed properties.
Interface:
@interface RangeArrayFactory : NSObject { NSRange range; } @end
Implementation:
- (id)initWithRange: (NSRange)aRange { self = [super init]; if (self) { range = aRange; } return self; }
Using:
NSRange range = NSMakeRange(5, 10); NSArray *syntheticArray = [[[RangeArrayFactory alloc] initWithRange: range] valueForKey: @"array"];
This solution is mainly for entertainment, but it may make sense for large ranges where a real array filled with sequential numbers will take up more memory than is really necessary.
As Rob Napier noted in the comments, you can also subclass NSArray , which simply requires you to implement count and objectForIndex: using the same code as countOfArray and objectInArrayAtIndex above.
source share