0

I try to call a C function from a dynamic library from Swift (3). I want to do mirror the function call as in the following c example.

This is the function declaration

extern int parseArgs(int argc, char **argv);

And this is the correct use of it:

char* args[] = {"-arg1","-arg2","-arg3"};
parseArgs (sizeof(args)/sizeof(char*), args);

So far, I have been able to convert Strings into the right format. This is what I am using.

typealias CHAR = UnsafeMutablePointer<Int8>
func cString(_ s: String) -> CHAR {
    return UnsafeMutablePointer<Int8>(mutating: (s as NSString).utf8String!)
}

So far, so good. Now, how do I continue?

typealias CHAR_ARRAY = UnsafeMutablePointer<CHAR?>

This seems to be the correct type. But I cannot do the instantiation. And knowing me and my lack of low level programming: even above CHAR solution is not ideally. How would you call the parseArgs function?

Wizard of Kneup
  • 1,863
  • 1
  • 18
  • 35

0 Answers0