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

时间:2016-07-08 21:05:18

标签: swift pointers swift3

我在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)

2 个答案:

答案 0 :(得分:6)

您可以像How to pass an array of Swift strings to a C function taking a char ** parameter中一样进行操作。由于不同,它有点不同 const - 参数数组的ness,并且因为有一个终止 nil(不得传递给strdup())。

这是它应该如何运作的:

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

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

答案 1 :(得分:0)

此类提供了一个与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($0)
            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
        }
    }
}