这个问题已经有了答案:
How to pass an array of Swift strings to a C function taking a char ** parameter
2个答案
我正在尝试创建一个C风格的字符指针数组,如下所示:

*const argv[]

我发现我必须使用UnsafePointer<UnsafeMutablePointer<Int8>>
但我不知道如何初始化它。
如何将此普通数组映射到UnsafePointer<UnsafeMutablePointer<Int8>>
let argv = ["/usr/bin/printf", "BBB"]

谢谢你

最佳答案

简单的方法是让cocoa为您形成c字符串:

let args = ["/usr/bin/printf","BBB"]
var cs = UnsafeMutablePointer<UnsafeMutablePointer<Int8>>.alloc(2)
for (ix,s) in args.enumerate() {
    cs[ix] = UnsafeMutablePointer<Int8>((s as NSString).UTF8String)
}
var cs2 : UnsafePointer<UnsafeMutablePointer<Int8>> = UnsafePointer(cs)

注意;cs不包含副本。它的指针指向args中的字符串。

关于swift - 如何快速声明char * const argv [],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33404598/

10-12 14:36