" to the expected argument type "UnsafePointer <_>" I work with an external C library in Swift ...">

Cannot convert value of type "UnsafePointer <Double>" to the expected argument type "UnsafePointer <_>"

I work with an external C library in Swift for OS X. I get the cda value, which is defined in C as double* (this is a pointer to a double array).

When imported into Swift, it recognizes the type as UnsafeMutablePointer. I am trying to convert this pointer and counter to a double array. Here's the code I'm using (suppose arrlen is the correct array count):

 let doublearrptr = UnsafePointer<Double>(cda) let xptarr = UnsafeBufferPointer<Double>(start: doublearrptr, count:arrlen) 

However, when compiling this piece of code, I get an error message:

 Cannot convert value of type 'UnsafePointer<Double>' to expected argument type 'UnsafePointer<_>' 

I'm relatively new to Swift, but I'm sure I can't convert it to UnsafePointer<_> . I tried converting to UnsafePointer<Void> , but got the same error. Swift recognizes that UnsafeMutablePointer<Double> is an UnsafeMutablePointer<Double> .

+5
source share
1 answer

So, I was able to solve this, albeit in a workaround.

I created a new convert function and used it:

 func convertArr<T>(count: Int, data: UnsafePointer<T>) -> [T] { let buffer = UnsafeBufferPointer(start: data, count: count) return Array(buffer) } ... let doublearrptr = UnsafePointer<Double>(cda) let arr = convertArr(Int(shobjarrlen), data: doublearrptr) 

For some reason this works, but not the original syntax ...

I'm still open to receiving answers on why my original syntax didn't work.

+3
source

All Articles