A type whose instances can be encoded, and appropriately passed, as elements of a C va
.
SDK
- Xcode 8.0+
Framework
- Swift Standard Library
Declaration
protocol CVarArg
Overview
You use this protocol to present a native Swift interface to a C “varargs” API. For example, a program can import a C API like the one defined here:
int c_api(int, va_list arguments)
To create a wrapper for the c
function, write a function that takes CVar
arguments, and then call the imported C function using the with
function:
func swiftAPI(_ x: Int, arguments: CVarArg...) -> Int {
return withVaList(arguments) { c_api(x, $0) }
}
Swift only imports C variadic functions that use a va
for their arguments. C functions that use the ...
syntax for variadic arguments are not imported, and therefore can’t be called using CVar
arguments.
If you need to pass an optional pointer as a CVar
argument, use the Int(bit
initializer to interpret the optional pointer as an Int
value, which has the same C variadic calling conventions as a pointer on all supported platforms.
Note
Declaring conformance to the CVar
protocol for types defined outside the standard library is not supported.