malloc'd C string created in c file being lost when bridging to swift file on specific input -
i making dictionary application written in swift , c. using c, .h , .c, search dictionary text file have specific traits, determined user inputs call dictionaryviewcontroller. method of passing input data c function , determining return char* fine however, on input, char* created not make swift file , nil value produced. how can sure swift can access memory allocated c file?
//.c file char* searchdictionary(char* u_input) { char* returnvalue = malloc(sizeof(char)*largenumber); //implementation return (returnvalue); } //.h file char* searchdictionary(char* u_input); //.swift file var uin = userinput.text let uinn = (uin nsstring).utf8string var uinnn = unsafemutablepointer<int8>(uinn) var out = unsafemutablepointer<int8>.alloc(1) out = searchdictionary(uinnn) //out gets value returned searchdictionary dictoutput.text = nsstring(cstring: out, encoding: nsutf8stringencoding)
i found out going on. when working c strings between swift , c, use nsasciistringencoding
. using nsutf8stringencoding
produce nil values specific input.
Comments
Post a Comment