I'm trying to interact with a C API from Swift and was able to successfully convert unsigned char* to String but am not able to convert back a String to unsigned char *
Here is my C function that returns the passed string:
unsigned char* test(unsigned char* hello) {
return hello;
}
Here is my swift code to call the c function which doesn't work:
let str = "hello, world!"
print(String(cString:test(str)))
Its goal is to pass "hello, world" to c and then have c pass "hello, world" back and then print it.
I've tried the solutions from here but they all seem outdated for Swift 4
Buffer attempt:
let buffer = UnsafeMutableBufferPointer.allocate(capacity: 4)
_ = buffer.initialize(from: 1...4)
print(String(cString: test(buffer)))
Error:
Cannot convert value of type 'UnsafeMutableBufferPointer' to expected argument type 'UnsafeMutablePointer!'
String
-to-pointer conversion is unsafe because its storage is only guaranteed to be valid for the duration of the call. If your C API actually does something like that, you won't be able to use automatic conversions. – zneakString
? – Weather Vane