I am trying to call a C function from Swift
One of the arguments to the C function is defined as the type:
const char * const * i_argv
In Xcode auto completion maps this to Swift type:
_ i_argv: UnsafePointer<UnsafePointer<Int8>?>!
All my attempts to create an instance of this type have failed
I want to pass an array of two Int8
let args: [Int8] = [ 1, 1 ]
If I pass this directly I get the error:
cannot convert value of type 'UnsafePointer<Int8>' to expected argument type 'UnsafePointer<UnsafePointer<Int8>?>'
So I then tried to wrap the array in an outer UnsafePointer
let argsp: UnsafePointer<Optional<UnsafePointer<Int8>>> = UnsafePointer(args)
But this does not appear to work as expected and gives the error:
cannot assign value of type 'UnsafePointer<Int8>' to type 'UnsafePointer<Optional<UnsafePointer<Int8>>>'
I can't figure out how to wrap the UnsafePointer type of the array with an outer UnsafePointer
Edit:
An example from the C project:
#define FIB_ARG_VALUE "40"
const char* i_argv[2] = { FIB_ARG_VALUE, NULL };
result = m3_CallWithArgs (f, 1, i_argv);
Following your comment I have managed to get the Swift code to compile with the following argument construction (just a compilation test)
let argsp: [UnsafePointer<Int8>?] = [ UnsafePointer([1,2,3]), UnsafePointer("40".cString(using: String.Encoding.utf8)) ]
This still gives a warning about creating dangling pointers