Oh, I don't know how I missed this, but there's a very convenient
-[NSArray pathsMatchingExtensions:] method already declared in
NSPathUtilities.h:
http://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/doc/uid/20000137-BBCHHAJJ

So your entire problem boils down to one method call.

--Kyle Sluder

On Thu, Jul 28, 2011 at 1:45 PM, Kyle Sluder <kyle.slu...@gmail.com> wrote:
> On Thu, Jul 28, 2011 at 1:35 PM, Chris Paveglio
> <chris_paveg...@yahoo.com> wrote:
>> I have an array of file paths, and I need to filter them to return only 
>> files with extensions I have in another array. So any files that end with 
>> {.tif, .png, .eps} etc.
>> I'm looking at using: - (NSArray 
>> *)filteredArrayUsingPredicate:(NSPredicate *)predicate
>> Is this the best option for this kind of filter? Is there any other way, 
>> aside from "doing it the long long way", i.e. enumerating over each's item's 
>> pathExtension in array1 through each file extension in array2?
>
> Actually, the "long way" might be your best bet after all.
>
> You could use -[NSArray indexesOfObjectsWithOptions:passingTest:] to
> enlist GCD to do a parallel filtering of all your strings:
>
> // warning: typed in mail client
> NSArray *validExtensions = [NSArray arrayWithObjects:@".tif", @".png",
> @".eps", nil];
> NSIndexSet *indexes = [myFilePaths
> indexesOfObjectsWithOptions:NSEnumerationConcurrent passingTest:^(id
> obj, NSUInteger idx, BOOL *stop) {
>  NSString *filePath = (NSString *)obj;
>  for (NSString *extension in validExtensions) {
>    if ([[string pathExtension] isEqualToString:extension])
>      return YES;
>  }
>
>  return NO;
> }];
>
> NSArray *filteredPaths = [myFilePaths objectsAtIndexes:indexes];
>
> --Kyle Sluder
>
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to