I need to send a Swift struct
consisting of two floats to some C++ code.
struct ComplexNumber
{
var real: Float
var imaginary: Float
}
void processComplexNumbers(const ComplexNumber *complexNumbers, const int *indices);
I have typedef'd the same struct in C++.
When I try to call this from Swift I get the error cannot convert value from UnsafePointer<ComplexNumber>
to expected argument type UnsafePointer<ComplexNumber>
. The following code is a simple illustration of this error.
var samples = [ComplexNumber]()
var indices = [Int]()
var samplesPtr = UnsafePoiner<ComplexNumber>(samples)
var indices = UnsafePoiner<Int>(indices)
processComplexNumbers(samples, indices)
Why can't I get this to work and is there a workaround?
Two problems:
I had to define an identical struct called ComplexNumbersF (F is for float) in C that I used for the unsafe pointer call.
var samples = [ComplexNumber]()
var samplesPtr = UnsafePoiner<ComplexNumberF>(samples)
If you want to use Swift's Int
the C++ equivalent is long
. If you use a C++ int
every other value will be 0. Alternatively you can use Swift's Int32
, but that wasn't really desirable in my case.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.