How can I pass a Swift string to a function c?
I am having serious problems passing a string from swift to a function written with c.
I am trying to do this in my swift code
var address = "192.168.1.2"
var port = 8888
initSocket(address, port)
The c function looks like this:
void initSocket(char *address, int port);
Im getting the error: Cannot convert expression of type "Void" to type "CMutablePointer"
I cannot find a solution that works.
+3
Mads Gadeberg
source
to share
1 answer
Swift CStrings works with constant C strings, so use
void initSocket(const char *address, int port);
instead of an argument char*
and declare the variable address
as CString:
var address: CString = "192.168.1.2";
+4
Wojtek Surowka
source
to share