To be clear I want to send my C function an array of null strings of fixed length like:
["\0\0\0\0\0\0", "\0\0\0\0\0\0", "\0\0\0\0\0\0"]
expect the array to return populated like:
["1.2345", "6.7891", "0.1112"]
[update ----------->]
I've seen in various places that people have had some success with copying the withArayOfCstrings() function from the SwiftPrivate code from the Swift source code repository. An explanation of why this works (at one time) is this highly cited source: https://oleb.net/blog/2016/10/swift-array-of-c-strings/ (tl;dr swift developers also needed a way to pass strings between Swift and C for QA.) However it seems this has stopped working as I've also seen no success from more recent asks. It seems 4 years ago this may have still worked. One post I ran into mentioned that they believed something changed in version 4?
The crux of the issue seems that a 2 dimensional array that used to be *'char * const ' in C used to be representable in Swift as [UnsafeMutablePointer?]. The multi-dimensional array now seems to be represented by a double pointer 'char ** ' in C and this now looks like UnsafeMutablePointer<UnsafeMutablePointer?> in Swift. This is where it seems everyone gets stuck and have no way of creating such an object to pass to be able to pass.
[ <----------- /update]
I've attached the GMP library to an Xcode Project and wanted to test out some functionality by sending an array of inputs, and read out an array of outputs. I just can't seem to figure out how to pass out an array of char* from C to Swift.
The error indicates I need a specific format. I've seen examples of just one char* but not an array of them and I can't fathom from that the correct way I construct a proper unsafe pointer for an array.
my C header
#define GMP_STR_MAXLEN 256
void get_gmp_calculations(const signed long int inputs_x[], const signed long int inputs_y[], int inputs_len, char *outputs[]);
my Swift code
func test_gmp(int_inputs_x: [Int], int_inputs_y: [Int])
{
var outputs: [[CChar]] = [[CChar]](repeating: [CChar](repeating: 0, count: Int(GMP_STR_MAXLEN)), count: int_inputs_x.count)
var unsafe_outputs: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>? = //??? something something "outputs"
get_gmp_calculations(int_inputs_x, int_inputs_y, Int32(int_inputs_x.count), unsafe_outputs)
withUnsafePointer(to:
APIs developer.apple.com/documentation/swift/…