NSArray subarrayWithIndexSet:
Something useful for NSIndexSet to do -- let you extract an arbitrary set of items from an NSArray.
You could use this to conveniently reduce the objects referenced by the current selection in a NSTableView to a new NSArray for schlepping onto the clipboard or whatever.
In this implementation, indices outside of the array bounds are silently ignored.
This code depends on autoreleasedCopy.
-------------- Interface -------------
(HKStringWithUnichar.h)
#import <Foundation/Foundation.h>
@interface NSArray (SubarrayWithIndexSet)
- (NSArray *)subarrayWithIndexSet: (NSIndexSet *)indices;
@end
-------------- Implementation -------------
#import "HKNSSubarrayWithIndexSet.h"
@implementation NSArray (SubarrayWithIndexSet)
- (NSArray *) subarrayWithIndexSet: (NSIndexSet *)indices
{
NSMutableArray * result = [NSMutableArray array];
unsigned count = [self count];
unsigned currentIndex = [indices firstIndex];
while ( currentIndex != NSNotFound )
{
if (currentIndex < count)
{
[result addObject: [self objectAtIndex: currentIndex]];
}
currentIndex = [indices indexGreaterThanIndex: currentIndex];
}
return [result autoreleasedCopy];
}
@end
• While fact-based, the contents of this website is 100% opinion • All advertisements Selected By Google Ad Sense • Your Mileage May Vary • Void Where Prohibited • Do Not Try This At Home • Don’t Be An Idiot •