In my swift app project, I have to interact with C APIs. One of the C function takes a char pointer as input, therefore I have to convert my swift string into char pointer. But I don't know how to do it properly.
Here's my sample code:
var user_id_ptr : UnsafeMutablePointer<CChar> = UnsafeMutablePointer.alloc(100)
var cusID = "123"
user_id_ptr.memory = cusID.cStringUsingEncoding(NSUTF8StringEncoding)!
And I always got this error:
Cannot assign a value of type '[CChar]' to a value of type 'CChar'
I also check the answer in: UnsafeMutablePointer<Int8> from String in Swift
Above answer is slightly different from mine. The example above is for only one character, but I have a string. Also, I have to pass multiple arguments to the function, I can't use something like let result = "N".withCString { dgeev_(UnsafeMutablePointer($0), UnsafeMutablePointer($0), &N, ...) }. Please show me some example for how to complete this.
char *
or aconst char *
, either stackoverflow.com/questions/27876179/… or stackoverflow.com/questions/27063569/… should solve your problem. – Martin Rlet result = "N".withCString { dgeev_(UnsafeMutablePointer($0), UnsafeMutablePointer($0), &N, ...) }
. Please show me some example for how to complete this. – Wei Wen Hsiao