[Swift vs C] Basic variables interaction
When you interact with Swift and C codes, you will be often confronted with some issues to convert variables.
You have to cast those variables to send/received from/to C, swift in a propper way.
here I simply introduce how to convert variables between Swift and C.
The example codes for C are often written as functions and return variables that may be received by swift.
int type
int of C
C
void NormalInt(int a)
{
a = 100;
return a;
}
swift
var insertValue:Int = 100 var SwiftValue:Int = Int(NormalInt(Int32(insertValue:Int)))
In this example, you have to convert int type of C to Int type of Swift and equally insertValue is converted to Int32 type of Swift which corresponds to int type of C
int 16 bit
C
void int16bit(int16_t a)
{
a = 100;
return a;
}
swift
var insertValue:Int = 100 var SwiftValue:Int = Int(NormalInt(Int16(insertValue:Int)))
int 32 bit
C
void int32bit(int32_t a)
{
a = 100;
return a;
}
swift
var insertValue:Int = 100 var SwiftValue:Int = Int(NormalInt(Int32(insertValue:Int)))
int 64 bit
C
void int64bit(int64_t a)
{
a = 100;
return a;
}
swift
var insertValue:Int = 100 var SwiftValue:Int = Int(NormalInt(Int64(insertValue:Int)))
float type
C
float floatfunc(float a)
{
return a;
}
Swift
var insertFloat:Float = 0.1 var varfloat:Float = floatfunc(insertFloat)
double type
C
float doublefunc(double a)
{
return a;
}
Swift
var insertDouble:Double = 0.1 var varDouble:Double = doublefunc(insertDouble)
It is not necessary to convert float and double types of C to Float and Double types of Swift.

0 コメント: