I answered a fairly similar question a few weeks ago. Answer below edited for this question.
In general, I avoid NSInvocation
for this kind of work. It tends to be a maintenance headache and, in particular, creates difficulty in refactoring in the future.
First, given this method:
-(void)plotPoly:(Polygon *)poly WithColor:(UIColor *)color AndFill:(BOOL)filled;
It would generally be declared as:
-(void)plotPoly:(Polygon *)aPoly color:(UIColor *)aColor filled:(BOOL)filledFlag;
This follows the naming conventions a bit more closely.
Now, what I would do is actually capture the arguments into a simple class that provides an -invoke
method.
Something with an interface like this:
PolyPlotter.h:
@interface PolyPlotter : NSObject
{
Polygon *poly;
UIColor *color;
BOOL filled;
}
+ plotterWithPoly: (Polygon *) aPoly color: (UIColor *) aColor filled: (BOOL) filledFlag;
- (void) plot;
@end
PolyPlotter.m:
@interface PolyPlotter()
@property Polygon *poly;
@property UIColor *color;
@property BOOL filled;
@end
@implementation PolyPlotter
@synthesize poly, color, filled;
+ plotterWithPoly: (Polygon *) aPoly color: (UIColor *) aColor filled: (BOOL) filledFlag;
{
PolyPlotter *polygonPlotter = [PolyPlotter new];
polygonPlotter.poly = aPoly;
polygonPlotter.color = aColor;
polygonPlotter.filled = filledFlag;
return [polygonPlotter autorelease];
}
- (void) plot;
{
// ... do your plotting here ...
}
@end
Usage is straightforward. Just create an instance of PolygonPlotter and tell it to perform the selector plot
after delay or on main thread or whatever.
Given the question, I suspect that you might need a bit more context at the time of drawing? If so, you could pass that information as an argument to -plot
by, say, declaring the method as:
- (void) plot: (UIView *) aViewToPlotIn;
Or something like that.
Like I said, slightly more code, but much more flexible and refactorable than the NSInvocation pattern. For example, you could quite easily make the PolygonPlotter something that could be archived.