osx - How to pass two Strings from Swift to a C++ function, and getting an array of Integers? -
i trying pass 2 strings
swift c++ function, handle them there, , return array of integers (probally vector), not sure yet.
i've tried (c++ function declaration):
void exmaple(char* one, char* two);
but not able pass strings swift c++.
then changed function little:
void example(void* one, void* two) { string &a = *reinterpret_cast<string*>(one); string &b = *reinterpret_cast<string*>(two); cout<<"string b is: "<<b<<endl; }
and tried calling function in swift:
let astr = unsafemutablepointer<string>.alloc(10) astr.initialize("stringa") let bstr = unsafemutablepointer<string>.alloc(10) bstr.initialize("stringb") example(astr, bstr)
but output is:
string b is:
how can pass 2 strings swift c++ function, , return array/vector of integers?
swift has magic bridging allows pass regular swift strings directly c functions. example, strlen
has signature size_t strlen(const char *)
, comes swift func strlen(_: unsafepointer<int8>) -> uint
can call ordinary strings this:
import darwin let greeting: string = "hello!" strlen(greeting) // returns 6 strlen("goodbye") // returns 7
and compiler automatically generate null-terminated c strings call.
the last part of question trickier unfortunately. not there no bridging c++, c , objective-c, there won't ability pass c++ vector. best bet either go via objective-c layer , pass nsarray
, or use c-style pass in empty buffer , have function write values it.