1

Having a c++ library, and using swift to call c++ function. But the difficult point is , this c++ function has char** parameter. I know how to bridge C++ code with swift code. But I don't know how to write c++ char** in swift so that swift can call c++ function.

This is c++ function I should call:

`int avcmdtest( int argc, char ** argv ){ }`

In Swift, I need something like this:

`var inputArgChar:Array<String>
 inputArgChar[0] = "some chars" 
 inputArgChar[1] = "some chars" 
 inputArgChar[2] = "some chars"
 avcmdtest(3, &inputArgChar)`

But, swift compiler said :

Cannot convert value of type Array(String) to expected argument type 'UnsafeMutablePointer(UnsafeMutablePointer(Int8>?>!'

Somebody can help me out? More appreciate if answer with some explainations.

1 Answers1

2

You need to create UnsafeMutablePointer. But please read this doc first becouse it is quite tricky to use.

Probably it could be like this:

var char: CChar =  CChar("1")!
let charPointer = UnsafeMutablePointer<CChar>(&char)

And remember when you are using UnsafeMutablePointer you should release memory manually.

Oleg Gordiichuk
  • 15,240
  • 7
  • 60
  • 100