Re: Handling UTIs from a Cocoa protocol
Re: Handling UTIs from a Cocoa protocol
- Subject: Re: Handling UTIs from a Cocoa protocol
- From: Charles Srstka <email@hidden>
- Date: Sat, 17 Dec 2011 23:13:48 -0600
On Dec 17, 2011, at 8:02 PM, C.W. Betts wrote:
> This is how I have my code set up: an Objective-C protocol that has a class function that returns an NSArray of UTIs that it can handle, and a member function that handles the file type:
>
> @protocol PcsxrFileHandle <NSObject>
>
> + (NSArray *)utisCanHandle;
> - (BOOL)handleFile:(NSString *)theFile;
>
> @end
>
> However, as my code is currently set up, the UTIs are used in a few special cases and are not called in determining what UTI opens what file. The code is set up like so:
>
> - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
> {
> NSString *utiFile = [[NSWorkspace sharedWorkspace] typeOfFile:filename error:nil];
> NSObject<PcsxrFileHandle> *hand = nil;
> BOOL isHandled = NO;
> if(UTTypeEqual(CFSTR("com.codeplex.pcsxr.plugin"), (CFStringRef)utiFile)) {
> hand = [[PcsxrPluginHandler alloc] init];
> isHandled = [hand handleFile:filename];
> } else if(UTTypeEqual(CFSTR("com.codeplex.pcsxr.memcard"), (CFStringRef)utiFile)) {
> hand = [[PcsxrMemCardHandler alloc] init];
> isHandled = [hand handleFile:filename];
> } else if(UTTypeEqual(CFSTR("com.codeplex.pcsxr.freeze"), (CFStringRef)utiFile)) {
> hand = [[PcsxrFreezeStateHandler alloc] init];
> isHandled = [hand handleFile:filename];
> } else if(UTTypeEqual(CFSTR("com.alcohol-soft.mdfdisc"), (CFStringRef)utiFile) || UTTypeEqual(CFSTR("com.apple.disk-image-ndif"), (CFStringRef)utiFile) || UTTypeEqual(CFSTR("public.iso-image"), (CFStringRef)utiFile) || UTTypeEqual(CFSTR("com.codeplex.pcsxr.cuefile"), (CFStringRef)utiFile)) {
> hand = [[PcsxrDiscHandler alloc] init];
> isHandled = [hand handleFile:HandleBinCue(filename)];
> }
> if (hand) [hand release];
> return isHandled;
> }
>
> The PcsxrPluginHandler, PcsxrMemCardHandler, PcsxrFreezeStateHandler, and PcsxrDiscHandler all implement the PcsxrFileHandle protocol.
> Is there a way to put classes into some sort of array to go through and check if the UTI of a file matches up to any of the UTIs that the class can handle?
Chris already answered your main question, but I’d just like to add that it’s probably better to use UTTypeConformsTo() instead of UTTypeEqual() to test UTIs. This way, in the hypothetical case that you encounter a subtype of one of those types, things will still work, whereas with the code above, it won’t.
Also, something like ‘supportedTypes’ or ‘supportedUTIs’ would probably be a clearer name than ‘utisCanHandle’.
Charles_______________________________________________
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