Hello!
It looks like parts of this answer may have been discussed by @eskimo and @John_McCall on another thread, but I also thought I would provide some insight here in case it helped.
On the C side of the codebase setup the C files and expose the needed functions to Swift using a bridging header. If you start a Swift project and add C or ObjC files to it Xcode will ask if you want to generate a bridging header for your project.
C_String.c
void key_function(char *p0, char **p1) {
printf("po: %s \n", p0);
printf("p1: %s \n", *p1);
}
As mentioned above, the bridging header is the place to expose C functions, or C header files with many functions, to Swift.
SwiftString-Bridging-Header.h
// Add C Function here or optionally add C headers here.
extern void key_function(char *p0, char **p1);
In the Swift side of the codebase I am suggesting working with an NSString
to provide a NULL terminated UTF8 representation of the string that UnsafeMutablePointer
can use to create CChar
's from and then send over to the key_function()
in C.
Main.swift
// Swift Strings
var key: NSString = "zxcvbnmqwertyui"
var error: NSString = "noerror"
// Convert the Swift strings to CChars (or Int8) that map to
var key_p0 = UnsafeMutablePointer<CChar>(mutating: key.utf8String)
var error_p1 = UnsafeMutablePointer<CChar>(mutating: error.utf8String)
// Pass the character pointers over to the key_function() function in C.
key_function(key_p0, &error_p1)
I hope this helps!