Reputation: 27211
I want to implement C library into my iOS project. I'm using swift language.
I have a function where the input parameter - where output values are stored - is ar usual C double array:
double ar[6];
///...
err = c_lib_func(ar);
If I initialize inside swift like var ar: [Double]
xCode says I have to use
UnsafeMutablePointer
. But inside the docs I haven't found how to initialize n-lenght array for UnsafeMutablePointer
. I just can do something like this:
var ar : UnsafeMutablePointer<Double>
. But I can understand how to initialize it as 6-length array. Please, help me.
If I'm using
ar = [Double]
err = c_lib_func(ar);
the xCode shows to me this error:
/Users/admin/Documents/projects/myApp/myApp/file.swift:46:46: Cannot convert value of type '[Double]' to expected argument type 'UnsafeMutablePointer'
Upvotes: 0
Views: 460
Reputation: 5666
In Swift, [Double]
is an array of double values which is not what you are after. If you want to initialize an UnsafeMutablePointer you can just use:
var ar = UnsafeMutablePointer<Double>.alloc(6)
Use ar.dealloc(6) to release the memory again.
Upvotes: 1