Re: Filter an array
Re: Filter an array
- Subject: Re: Filter an array
- From: Wade Tregaskis <email@hidden>
- Date: Fri, 29 Jul 2011 09:09:43 -0700
> Further to earlier answers, bear in mind you've got no guarantee that file extensions are correct, or even exist. Plus of course, you might have both .jpg and .jpeg. You might well be better iterating through, finding the UTI of each file, and working from that.
Oooh, that's kind of embarrassing. :) Yes, that's a very good point, that we all should have realised before worrying about the details of file extension comparison. So I'll retract my prior recommendations and instead suggest using something like:
// Provided parameters:
//
// paths: Collection of NSStrings, the absolute paths of the items in question.
// supportedTypes: Collection of UTIs (NSStrings) that you're looking for.
for (NSString *path in paths) {
NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
NSString *uti = nil;
NSError *error = nil;
if ([url getResourceValue:&uti forKey:NSURLTypeIdentifierKey error:&error]) {
for (NSString *supportedType in supportedTypes) {
if (UTTypeConformsTo(uti, supportedType)) {
// You have a match.
}
}
} else {
// Handle error
}
}
There's a lot of nasty enumeration there, rather than hashtables which make me much happier, but I don't know of a better way off hand.
However, -[NSURL getResourceValue:forKey:error:] is only available in iOS 4.0 and later. So you'll have to find another route if you're targeting earlier than that.
Also, if your paths are actually the contents of a folder (or folders), you could use -[NSFileManager enumeratorAtURL:includingPropertiesForKeys:options:errorHandler:], which will handle the enumeration for you as well as possibly being faster - one of the promised potentials of that method is that, since you tell it up front what file properties you're interested in, it can pre-fetch and/or batch-fetch them. Again, though, that's only available on iOS starting at version 4.0.
Addendum: See also the Uniform Type Identifiers Overview for more information on UTIs. You'll note, for example, that I didn't just do isEqualToString: when comparing them, and for good reason - all explained in the docs.
_______________________________________________
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