Re: Sublclassing NSThread
Re: Sublclassing NSThread
- Subject: Re: Sublclassing NSThread
- From: Jean-Daniel Dupas <email@hidden>
- Date: Tue, 16 Dec 2008 19:49:41 +0100
Le 16 déc. 08 à 18:29, Brad O'Hearne a écrit :
Hello,
I am trying to create an NSThread subclass which completely wraps
the desired behavior of the thread execution. Now typically when
creating a new thread instance, it seems one will use the
initWithTarget:selector:object initializer to properly direct the
thread's execution. But what if I want the thread subclass to invoke
a method within itself? This leaves you with a situation where you
have to set the target to self within the initializer, but that
seems problematic, as "self" is not yet defined, i.e.:
- (id)init {
self = [super initWithTarget:self @selector(myMethod) object:nil];
return self;
}
As you can see, the initWithTarget: param is set to self, but the
purpose of that statement is to set self, and self isn't defined yet
to my knowledge. I'd otherwise just use the NSThread's init method,
but I do not see a way to set the target as a property on the
thread object once initialized. So my question is, in Objective C,
is the typical approach to wrapping behavior entirely within an
NSThread subclass to override the start method, as such:
- (void) start {
[super start];
[self myMethod];
}
Is this the recommended approach, or is there another way to go
about this?
No. In Cocoa you never subclass NSThread. Instead of overriding start,
you implement you own start wherever you want (and with the name you
want) and you pass it as parameter (SEL + target).
_______________________________________________
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