Thotz/Thotz.html
 

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