【问题标题】:Convert a Swift Array of String to a to a C string array pointer将 Swift 字符串数组转换为 C 字符串数组指针
【发布时间】:2025-11-28 13:10:02
【问题描述】:

我使用的是 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)

【问题讨论】:

标签: swift pointers swift3


【解决方案1】:

您可以像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)) }

【讨论】:

  • 谢谢!像魅力一样工作:]
  • 在调用命令 API 时出现错误...使用未解析的标识符“命令”;你的意思是“doCommand”吗?
  • @Sunilaruru:问题是关于调用现有 C 函数command
  • var tool = "/bin/rm" let array: [String?] = ["name1", "name2", nil] // 创建 [UnsafePointer]: var cargs = array .map { $0.flatMap { UnsafePointer(strdup($0)) } } // 调用 C 函数:let result = command(&cargs) // 释放重复的字符串:for ptr in cargs { free(UnsafeMutablePointer(mutating: ptr)) } var 管道:文件? = nil status = AuthorizationExecuteWithPrivileges(authRef!, tool, authFlags, cargs, &pipe);没事吧?
【解决方案2】:

这个类提供了一个与 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
        }
    }
}

【讨论】: