Re: Programmatically constructing list of variable arguments?
Re: Programmatically constructing list of variable arguments?
- Subject: Re: Programmatically constructing list of variable arguments?
- From: "Jonathan del Strother" <email@hidden>
- Date: Fri, 12 Dec 2008 14:41:54 +0000
On Fri, Dec 12, 2008 at 2:14 PM, Kyle Sluder
<email@hidden> wrote:
> On Fri, Dec 12, 2008 at 9:12 AM, Kyle Sluder
> <email@hidden> wrote:
>> On Fri, Dec 12, 2008 at 8:53 AM, Jonathan del Strother
>>> The number of arguments isn't known at compile-time. I'd like to be
>>> able to take an NSArray of the arguments, and programmatically convert
>>> that to something suitable for invoking the method.
>
> Wait, I seem to have misunderstood you. Do you want to take a known
> set of arguments and pass that off to a variadic function?
>
Maybe an example would be helpful. Let's say I want to call
-[Bartender mixCocktailIngredients:(NSString*)ingredient ...], and I
want to call that with different arguments depending on the user's
preferences.
One way of doing so would be to use an if-statement, and just type out
all the possibilities:
if (user.likesTequila)
[bartender mixCocktailIngredients:@"gin", @"vodka", @"tequila", nil];
else
[bartender mixCocktailIngredients:@"gin", @"vodka", nil];
Which works fine, but rapidly expands into a huge if-statement as you
try and handle more states. Ideally I'd like to do something along
the lines of:
NSMutableArray* ingredients = [NSMutableArray arrayWithObjects:@"gin",
@"vodka", nil];
if (user.likesTequila)
[ingredients addObject:@"tequila"];
[bartender mixCocktailIngredients: NSArrayToVariableList(ingredients)];
I started to come up with a variable-argument proxy that was supposed
to be invoked like :
[[bartender vaProxy] mixCocktailIngredients:ingredients];
and then used forwardInvocation to expand the NSArray... but then
discovered that NSInvocation doesn't support variable arguments.
_______________________________________________
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