split up alloc and init?
split up alloc and init?
- Subject: split up alloc and init?
- From: John Terranova <email@hidden>
- Date: Mon, 11 Feb 2008 00:04:15 -0800
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