c - Array of Swift Strings into const char * const * -
i having trouble converting array of swift strings array c function signature:
pgconn *pqconnectstartparams(const char * const *keywords, const char * const *values, int expand_dbname)
in swift, const char * const * shows as:
<unsafepointer<unsafepointer<int8>>
so try convert contents of dictionary [string:string] called 'options' , feed function follows:
var keys = [[int8]]() var values = [[int8]]() (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 , runs, function not work.
any insight appreciated.
that's not perfect @ least works.
let options = ["key1": "value1", "key2": "value2", "key3": "value3", "key4": "value4"] var keys = [string]() (key, value) in options { keys.append(key) } //you need identify how many paramenters should provided , set them following "static way" //i did not find how prepare dynamically let ckey1 = keys[0].cstringusingencoding(string.defaultcstringencoding())! let key1pointer = unsafepointer<cchar>(ckey1) let ckey2 = keys[1].cstringusingencoding(string.defaultcstringencoding())! let key2pointer = unsafepointer<cchar>(ckey2) let ckey3 = keys[2].cstringusingencoding(string.defaultcstringencoding())! let key3pointer = unsafepointer<cchar>(ckey3) let ckey4 = keys[3].cstringusingencoding(string.defaultcstringencoding())! let key4pointer = unsafepointer<cchar>(ckey4) let keyscarray = [key1pointer, key2pointer, key3pointer, key4pointer] f(keyscarray) /* c - code void f(const char * const *keywords) { printf("%s\n", keywords[0]); printf("%s\n", keywords[1]); printf("%s\n", keywords[2]); printf("%s\n", keywords[3]); } */
hope helps. can share sample app if want.
Comments
Post a Comment