Leonardo
Leonardo

Reputation: 11389

How to create variadic function? (Can take any number of arguments)

I was wondering if there is a similar way to do this (C# definition) in Objective-C:

public void MyWorkingMethod (string Argument1, params int numbers)

It can be called like MyWorkingMethod("a") or MyWorkingMethod("b", 1, 2, 3).

I'm trying to implement the string.Format as C# does in Objective-c.

Upvotes: 1

Views: 295

Answers (3)

Legolas
Legolas

Reputation: 12325

It should be something like

 - (void) MyWorkingMethod : ( NSString * ) Argument1 secondInput:(NSArray *) numbers {
 }

And you will call the function with

[self MyWorkingMethod:@"Hello World" secondInput:numbers];

Upvotes: -1

sidyll
sidyll

Reputation: 59287

What exactly string.Format does? If you need a function, declare and define it as you'd do in C (including variadic cases):

void MyWorkingMethod (NSString *string, int numbers)

If it's related to formatting a string, have you checked NSString's stringWithFormat:? What about libc sprintf?

Upvotes: 0

Nathanial Woolls
Nathanial Woolls

Reputation: 5291

Note that there is already a stringWithFormat method that is very similar to string.Format found in the .NET Framework. That said, you can definitely have a variable number of arguments in an Objective-C method. See this link for details.

Upvotes: 5

Related Questions