I am writing swift code & need to invoke a C function.
var idx = 5
//ERROR: Cannot invoke c_func with argument list of type '(Int)'
c_func(idx)
The C function signature is:
void c_func(int idx)
So, my question is how can I pass swift Int
to C function which accepts int
as parameter?
The C type int
is mapped to Swift as
/// The C 'int' type.
typealias CInt = Int32
Therefore you have to declare the variable as CInt
:
var idx : CInt = 5
c_func(idx)
or convert the Int
to CInt
:
var idx = 5
c_func(CInt(idx))
(Of course you can use Int32
instead if you prefer to emphasize that it is a fixed-size integer type.)
More information about the mappings between (Objective-)C types and Swift types can be found in the "Using Swift with Cocoa and Objective-C" documentation.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments