Re: split up alloc and init?
Re: split up alloc and init?
- Subject: Re: split up alloc and init?
- From: Scott Andrew <email@hidden>
- Date: Mon, 11 Feb 2008 00:46:36 -0800
Wouldn't it be better to make it a wrapper around invocation object?
Ten the problem goes away. You could then have a function
-(InvocationObject*) invocationObject;
or even a static init that returnes an auto released invocation object
that has a single instance of your op class. Something like..
@interface DDLThumbMakerOp
{
NSInteger pageIndex;
CGFloat thumbSize;
}
@property(assign, readwrite) NSInteger pageIndex;
@property(assign, readwrite) CGFloat thumbSize;
-(InvocationObject*) invocationObjectForTarget:(id)target selector:
(SEL)selector;
@end
You call can then use.
DDLThumbMakerOp *opBig = [[[DDLThumbMakerOp alloc] init] autorelease];
opBig.pageIndex = index;
opBig.thumbSize = size;
NSInvocationObject* invocationObject = [opBig
invocationObjectForTarget:self
selector:@selector(threadedThumbWithData)];
[opQ addOperation: invocationObject];
The invocation creation would be.
-(InvocationObject*) invocationObjectForTarget:(id)target selector:
(SEL)selector
{
return [[NSInvocationObject alloc] initWithTarget:target
selector:selector object:self];
}
This will make your DLLThumbMakerOp still a parameter of the selector.
And i think its a bit easier on the eyes, at least to me and
encapsulated nicely.
Scott
On Feb 11, 2008, at 12:04 AM, John Terranova wrote:
The self = [super init] discussion is interesting. Here is
something related that I came across just before the [super init]
discussion started. Basically, is it safe to split up alloc and init?
[NSInvocationOperation initWithTarget:selector:object:] takes an id
as the object parameter that will eventually get passed to the
selector parameter. What if I want to pass the operation object
itself to the selector? There is no setObject: to set it after
initializing. I must pass the object to init.... The only way to
do that is to split up the alloc and init.
Is this not the same question as assigning [super init]? Can I
count on the object that alloc returns being the exact same object
after init... returns?
Here are the details. I subclass NSOperation like this:
@interface DDLThumbMakerOp : NSInvocationOperation
{
NSInteger pageIndex;
CGFloat thumbSize;
}
@property(assign, readwrite) NSInteger pageIndex;
@property(assign, readwrite) CGFloat thumbSize;
@end
Then I want to create my operation object like this:
// first create the object
DDLThumbMakerOp *opBig = [DDLThumbMakerOp alloc];
// now initialize the operation object, passing itself as the
'object' parameter that will eventually be passed to
threadedThumbWithData:
id opReturned = [opBig initWithTarget:self
selector:@selector(threadedThumbWithData:) object:opBig];
NSAssert(opReturned == opBig, @"initWithTarget changed the object on
me! Crash and burn!");
opBig.pageIndex = index;
opBig.thumbSize = size;
[opQ addOperation: opBig];
I do this so that the operation object itself can be passed to the
threaded method and the method can extract necessary data out of the
operation object. This avoids the need to create a separate object
just to pass data to the threaded method.
The threaded method starts by getting the data out of the operation
object like this, then goes about its business:
- (NSImage *)threadedThumbWithData:(DDLThumbMakerOp *)opBig
{
NSInteger index = opBig.pageIndex;
NSRect frame;
frame.origin = NSZeroPoint;
frame.size.height = frame.size.width = opBig.thumbSize;
. . .
}
So, how dangerous is splitting up alloc and init, like this?
john
_______________________________________________
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
_______________________________________________
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