sample.swift
import Foundation
let options = ["-m" : 2, "-n" : 3]
var args : [String] = []
for (option_name, value) in options {
args.append(contentsOf: [option_name, value])
}
var cargs: [UnsafeMutablePointer<Int8>?] = args.map { strdup($0) }
initialize(Int32(args.count), &cargs)
for ptr in cargs { free(ptr) }
sample.c
void initialize(int argc, char **argv) {
//various
}
See: https://developer.apple.com/documentation/swift/unsafemutablepointer
sample.swift
let foo = get_foo()
let result = String(cString: foo!) // TODO:nil should be safe
print(result)
sample.c
char* get_foo() {
//return
}
Recommended Posts