I am having trouble converting an array of Swift Strings into an array for a c function with the signature:
PGconn *PQconnectStartParams(const char * const *keywords, const char * const *values, int expand_dbname)
In Swift, the const char * const * shows up as:
<UnsafePointer<UnsafePointer<Int8>>
So I try to convert the contents of a Dictionary [String:String] called 'options' and feed that to the function as follows:
var keys = [[Int8]]()
var values = [[Int8]]()
for (key, value) in options {
var int8Array = key.cStringUsingEncoding(NSUTF8StringEncoding)!
keys.append(int8Array)
int8Array = value.cStringUsingEncoding(NSUTF8StringEncoding)!
values.append(int8Array)
}
pgConnection = PQconnectStartParams(UnsafePointer(keys), UnsafePointer(values), 0)
It compiles and runs, but the function does not work.
Any insight would be greatly appreciated.
PQconnectdbParams()yet you are calling a function namedPQconnectStartParams()...?const char * const *keywords.keywordsis a pointer to aconstpointer to aconst char(which is the same aschar const, both mean "constant character"). This means you are not looking for a single string ("null terminated character array"), yet you probably are looking for an array of strings. Kind of likechar **argv. I don't know much about Swift but you'd have to figure out how to convert a Swift string into a C null-terminated character array, and then have to find out how to have an array of those convert into a C array of strings, I think..cStringUsingEncoding(NSUTF8StringEncoding)returnint8array? I'm confused how the strings and theint8thing relate?