将 Swift 字符串数组转换为 C 字符串数组指针

Convert a Swift Array of String to a to a C string array pointer

我在 Swift 3,我需要与 C API 交互,它接受以 NULL 结尾的字符串列表,例如

const char *cmd[] = {"name1", "value1", NULL};
command(cmd);

在 Swift 中,API 被导入为

func command(_ args: UnsafeMutablePointer<UnsafePointer<Int8>?>!)

在使用类型转换或 unsafeAddress(of:) 尝试数百次后,我仍然无法完成这项工作。即使我传递了一个通过编译的有效指针,它也会在运行时崩溃,提示内存访问无效(在 strlen 函数中)。或者可能是关于 ARC 的问题?

let array = ["name1", "value1", nil]

// ???
// args: UnsafeMutablePointer<UnsafePointer<Int8>?>

command(args)

您可以像 中那样进行操作。因为不同所以有点不同 const-参数数组的ness,并且因为有一个终止 nil(不得传递给 strdup())。

它应该是这样工作的:

let array: [String?] = ["name1", "name2", nil]

// Create [UnsafePointer<Int8>]:
var cargs = array.map { [=10=].flatMap { UnsafePointer<Int8>(strdup([=10=])) } }
// Call C function:
let result = command(&cargs)
// Free the duplicated strings:
for ptr in cargs { free(UnsafeMutablePointer(mutating: ptr)) }

这个 class 提供了一个与 char** 一起工作的指针并自动释放内存,即使它是一个结构(使用带有释放器的映射数据的小技巧)。

public struct CStringArray {
    public let pointer: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>
    public let count: Int
    private var data: Data

    public init(_ array: [String]) {
        let count = array.count

        // Allocate memory to hold the CStrings and a terminating nil
        let pointer = UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>.allocate(capacity: count + 1)
        pointer.initialize(repeating: nil, count: count + 1)  // Implicit terminating nil at the end of the array

        // Populate the allocated memory with pointers to CStrings
        var e = 0
        array.forEach {
            pointer[e] = strdup([=10=])
            e += 1
        }

        // This uses the deallocator available on the data structure as a solution to the fact that structs do not have `deinit`
        self.data = Data(bytesNoCopy: pointer, count: MemoryLayout<UnsafeMutablePointer<CChar>>.size * count, deallocator: .custom({_,_ in
            for i in 0...count - 1 {
                free(pointer[i])
            }
            pointer.deallocate()
        }))

        self.pointer = pointer
        self.count = array.count
    }

    public subscript(index: Data.Index) -> UnsafeMutablePointer<CChar>? {
        get {
            precondition(index >= 0 && index < count, "Index out of range")
            return pointer[index]
        }
    }

    public subscript(index: Data.Index) -> String? {
        get {
            precondition(index >= 0 && index < count, "Index out of range")
            if let pointee = pointer[index] {
                return String(cString: pointee)
            }

            return nil
        }
    }
}