Supposing I have a class in Objective-c with a static method like this:
+ (NSError *)executeUpdateQuery:(NSString *)query, ...;
How do I ca
CVArgTypeis useful in presenting C "varargs" APIs natively in Swift. (Swift Docs)
If you have
+ (int)f1:(int)n, ...;
you first need to make a va_list version:
+ (int)f2:(int)n withArguments:(va_list)arguments
This can be done without duplicating code by calling the va_list version from the variadic version. If you didn't write the original variadic function it may not be possible (explained in this reference).
Once you have this method, you can write this Swift wrapper:
func swiftF1(x: Int, _ arguments: CVarArgType...) -> Int {
return withVaList(arguments) { YourClassName.f2(x, withArguments :$0) }
}
Note the omitted external parameter name (_ before arguments), which makes the call syntax for swiftF1 just like a normal C variadic function:
swiftF1(2, some, "other", arguments)
Note also that this example doesn't use getVaList because the docs say it is "best avoided."
You can further put this function in a Swift extension of the original class, if you want.