You're still instantiating every handler class just to see whether one applies
to a given file.
This is exactly why class methods exist: You can implement a class method on
your handler classes like "Can this handler class be used for files of this
type?" Then, for the class answers yes, that's the one you +alloc/-init to
actually keep around as the handler for a specific file.
-- Chris
On Dec 18, 2011, at 3:42 PM, C.W. Betts wrote:
> Thanks for the input everyone.
>
> For those curious, this is the final code:
> - (BOOL)application:(NSApplication *)theApplication openFile:(NSString
> *)filename
> {
> NSError *err = nil;
> NSString *utiFile = [[NSWorkspace sharedWorkspace] typeOfFile:filename
> error:&err];
> if (err) {
> NSRunAlertPanel(NSLocalizedString(@"Error opening file",nil),
> [NSString stringWithFormat:NSLocalizedString(@"Unable to open %@: %@", nil),
> [filename lastPathComponent], [err localizedFailureReason]], nil, nil, nil);
> return NO;
> }
> NSArray *handlers = [NSArray arrayWithObjects:[PcsxrPluginHandler
> class], [PcsxrMemCardHandler class], [PcsxrFreezeStateHandler class],
> [PcsxrDiscHandler class], nil];
> BOOL isHandled = NO;
> for (Class fileHandler in handlers) {
> NSObject<PcsxrFileHandle> *hand = [[fileHandler alloc] init];
> BOOL canHandle = NO;
> for (NSString *uti in [fileHandler supportedUTIs]) {
> if ([[NSWorkspace sharedWorkspace] type:utiFile
> conformsToType:uti]) {
> canHandle = YES;
> }
> }
> if (canHandle) {
> isHandled = [hand handleFile:HandleBinCue(filename)];
> }
> [hand release];
>
> }
> return isHandled;
> }
>
>
> On Dec 18, 2011, at 12:22 PM, Charles Srstka wrote:
>
>> You can just send the alloc/init message directly to the class object.
>>
>> Charles
>>
>> On Dec 18, 2011, at 11:01 AM, C.W. Betts wrote:
>>
>>> Let me see if I got this right. Create an NSArray with classes like this:
>>> [NSArray arrayWithObjects:[ClassName1 class], [ClassName2 class], nil]
>>> Then how would I call it? Would [[[anArray objectAtIndex:i] alloc] init]
>>> work? Or would I have to use a pure C method, something along the lines of
>>> getIdFromClass()?
>>> On Dec 18, 2011, at 2:20 AM, Charles Srstka wrote:
>>>
>>>> On Dec 18, 2011, at 3:14 AM, Ken Thomases wrote:
>>>>
>>>>> On Dec 18, 2011, at 3:06 AM, Charles Srstka wrote:
>>>>>
>>>>>> On Dec 18, 2011, at 2:49 AM, Ken Thomases wrote:
>>>>>>
>>>>>>> On Dec 18, 2011, at 2:36 AM, Charles Srstka wrote:
>>>>>>>
>>>>>>>> On Dec 18, 2011, at 2:31 AM, C.W. Betts wrote:
>>>>>>>>
>>>>>>>>> So I would do something along the lines of [NSArray
>>>>>>>>> arrayWithObjects:ClassName1, ClassName2, nil]?
>>>>>>>>
>>>>>>>> Or just class1, class2, etc. where class1 and class2 are both of type
>>>>>>>> Class.
>>>>>>>
>>>>>>> You can use pointers to class objects, but you can't just use class
>>>>>>> names. If you are starting from class names, you use [ClassName1
>>>>>>> class] to get the class object.
>>>>>>
>>>>>> Well, you *could* just use class names, if you used NSClassFromString()
>>>>>> before using the class. There wouldn’t be much point in doing that,
>>>>>> though, since class objects can fit inside arrays and would be more
>>>>>> convenient to use here.
>>>>>
>>>>> Did you mean for this to be off-list? Anyway, I took his use of
>>>>> ClassName1 to mean an identifier. After all, he didn't write
>>>>> @"ClassName1”.
>>>>
>>>> Nope, sorry, that was meant to be on-list. You’re probably right — I had
>>>> assumed that those were meant to be NSString variables, since that’s what
>>>> you’d need to be using if you were storing class names. Wasn’t thinking
>>>> from an Obj-C newbie perspective there; sorry. At any rate, getting the
>>>> class object by calling +class is, of course, the correct thing to do.
>>>>
>>>> Charles
>>>>
>>>> _______________________________________________
>>>>
>>>> Cocoa-dev mailing list ([email protected])
>>>>
>>>> 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/computers57%40hotmail.com
>>>>
>>>> This email sent to [email protected]
>>>>
>>>
>>
>>
>
> _______________________________________________
>
> Cocoa-dev mailing list ([email protected])
>
> 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/cmh%40me.com
>
> This email sent to [email protected]
_______________________________________________
Cocoa-dev mailing list ([email protected])
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 [email protected]