Re: @selector not working with (id)anObject
Re: @selector not working with (id)anObject
- Subject: Re: @selector not working with (id)anObject
- From: Trygve Inda <email@hidden>
- Date: Sun, 12 Apr 2009 00:32:08 +0000
- Thread-topic: @selector not working with (id)anObject
> On Apr 11, 2009, at 5:20 PM, Trygve Inda wrote:
>> -(void)threadMethod:(id)anObject
>> {
>> NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
>> SEL comp = (SEL)anObject;
>>
>> ... Do stuff ...
>>
>> [self performSelectorOnMainThread:comp withObject:nil
>> waitUntilDone:NO];
>> [pool release];
>> }
>>
>> This crashes during detachNewThreadSelector. How can I pass a method
>> to call
>> at the end of the thread processing as above?
>
> There are a handful of issues here:
>
> - a SEL is not an (id); if anything tries to -retain the SEL it is
> going to crash
>
> - in general, casting outside of casting up/down class hierarchies is
> an indication that your code is wrong
>
> If you really want to pass a selector around in a slot that normally
> holds objects, use NSStringFromSelector() and NSSelectorFromString().
>
> However, with that said, that is generally an atypical design
> pattern. Not totally odd, but just not that typical. Raises a the
> "what are you trying to do?" flag, though.
I have a thread. Depending on how it is called, I need to do something
different at the end of the main processing so I wanted to pass a selector
into the thread so it could call it when it was ready.
I could just pass an NSNumber and do case1, case2 etc, but thought it'd be
nice to just do the equivalent of passing a function pointer.
So one of these needs to be called:
[self performSelectorOnMainThread:@selector(cacheComplete) withObject:nil
waitUntilDone:NO];
[self performSelectorOnMainThread:@selector(initComlete) withObject:nil
waitUntilDone:NO];
Thoughts?
_______________________________________________
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