Passing in Variable Number of Args from One Function to Another in Swift

Passing in variable number of args from one function to another in Swift

sumOf and averageOf both take a variadic paramter of Int, which is not the same as an Array of Int. The variadic paramter is converted into an Array, though which is what numbers it, but then you aren't able to call sumOf on numbers.

You can fix this by making a sumOf function that takes a variadic parameter and one that takes an arrary like this:

func sumOf(numbers: Int...) -> Int {
return sumOf(numbers)
}
func sumOf(numbers: Int[]) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}

Swift function with args... pass to another function with args

Similar as in (Objective-)C, you cannot pass a variable argument list
directly to another function. You have to create a CVaListPointer
(the Swift equivalent of va_list in C) and call a function which
takes a CVaListPointer parameter.

So this could be what you are looking for:

extension String {
func getLocalizeWithParams(args : CVarArgType...) -> String {
return withVaList(args) {
NSString(format: self, locale: NSLocale.currentLocale(), arguments: $0)
} as String
}
}

withVaList() creates a CVaListPointer from the given argument list
and calls the closure with this pointer as argument.

Example (from the NSString documentation):

let msg = "%@:  %f\n".getLocalizeWithParams("Cost", 1234.56)
print(msg)

Output for US locale:

Cost:  1,234.560000

Output for German locale:

Cost:  1.234,560000

Update: As of Swift 3/4/5 one can pass the arguments to

String(format: String, locale: Locale?, arguments: [CVarArg])

directly:

extension String {
func getLocalizeWithParams(_ args : CVarArg...) -> String {
return String(format: self, locale: .current, arguments: args)
}
}

Passing an array to a function with variable number of args in Swift

Splatting is not in the language yet, as confirmed by the devs. Workaround for now is to use an overload or wait if you cannot add overloads.

passing unknown number of arguments in a function

Swift Variadic Parameters accepts zero or more parameters of a specified type. Syntax of variadic parameters is, insert three period characters (...) after the parameter’s type name.

func anyNumberOfTextField(_ textField: UITextField...) {

}

Now you can pass any number of textField.

anyNumberField(UITextField(),UITextField(), UITextField(), UITextField())

Note: A function may have at most one variadic parameter.

For more info check this Swift Functions

There is another way you can do that is called Array Parameter. There are some pros and cons of these two methods. You will find the difference here link.

how to pass variable arguments to another method?

AFAIK ObjectiveC (just like C and C++) do not provide you with a syntax that allows what you directly have in mind.

The usual workaround is to create two versions of a function. One that may be called directly using ... and another one called by others functions passing the parameters in form of a va_list.


..
[obj aMethod:@"test this %d parameter", 1337);
[obj anotherMethod:@"test that %d parameter", 666);
..

-(void) aMethod:(NSString *)a, ...
{
va_list ap;
va_start(ap, a);

[self anotherMethod:a withParameters:ap];

va_end(ap);
}

-(void) anotherMethod:(NSString *)a, ...
{
va_list ap;
va_start(ap, a);

[self anotherMethod:a withParameters:ap];

va_end(ap);
}

-(void) anotherMethod:(NSString *)a withParameters:(va_list)valist
{
NSLog([[[NSString alloc] initWithFormat:a arguments:valist] autorelease]);
}

Passing lists from one function to another in Swift

The numbers: Int... parameter in sumOf is called a variadic parameter. That means you can pass in a variable number of that type of parameter, and everything you pass in is converted to an array of that type for you to use within the function.

Because of that, the numbers parameter inside average is an array, not a group of parameters like sumOf is expecting.

You might want to overload sumOf to accept either one, like this, so your averaging function can call the appropriate version:

func sumOf(numbers: [Int]) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
func sumOf(numbers: Int...) -> Int {
return sumOf(numbers)
}

How to pass values or data between two function with parameters in swift?

Actually I didn't understand properly your problem. But as i understand that you can use this.

func firstFunc(lat: Double, long: Double) {
//do something
secondFunc(lat: lat, long: long)
}

func secondFunc(lat: Double, long: Double) {
//do something
}

But if you want to use values after process from secondFunc you can use inout parameters.

func firstFunc(lat: Double, long: Double) {
//do something
var newLat = lat
var newLong = long
secondFunc(lat: &newLat, long: &newLong)
//new values for newLat and newLong after secondFunc
}

func secondFunc(lat: inout Double, long: inout Double) {
//do something
}


Related Topics



Leave a reply



Submit