How to call C variadic functions from Swift?
In this article we’re going to create a Swift wrapper function for a C function that use the ...
syntax for variadic arguments:
int my_sum(int n, ...); |
...
variadic arguments syntax is not supported by default in Swift, you will get compiler errors like this:
'my_sum' is unavailable: Variadic function is unavailable |
There’s a way to workaround this limitation, if you can modify the C function to take va_list
as parameter.
Create a new C function with va_list
The original function looks like this:
int my_sum(int n, ...) |
To avoid duplicating the logic, we can create a new version take a va_list
as parameter, and call the new version from the original function:
// New version |
Create a Swift wrapper with CVarArg
func mySwiftSum(_ n: Int32, _ arguments: CVarArg...) -> Int32 { |
Now we can use this function in Swift:
let sum = mySwiftSum(5, 1, 2, 3, 4, 5) |