Re: Filter an array
Re: Filter an array
- Subject: Re: Filter an array
- From: email@hidden
- Date: Thu, 28 Jul 2011 14:15:47 -0700
> 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.
The docs don't state whether it's case sensitive or not. It also takes the list as an NSArray rather than an NSSet, which is suboptimal.
>
>> // 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;
>> }
>>
If you were to go this approach, which I wouldn't recommend given the existence of the convenience method above (assuming it's properly case-insensitive), do:
1) Use caseInsensitiveCompare:, not isEqualToString:.
2) Consider using an NSSet for the set of accepted extensions (though that will mean ensuring they're all e.g. lowercase, both when inserting the accepted values into the set and prior to checking for set membership).
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden