一尘不染

将字符串转换为int8数组

swift

我有一个C结构(旧的库,等等),其中包含一个C字符串,现在我需要将CFString和Swift字符串转换为该C字符串。就像是

struct Product{
   char name[50];
   char code[20];
}

所以我正在尝试将其分配为

productName.getCString(&myVarOfStructProduct.name, maxLength: 50, encoding: NSUTF8StringEncoding)

但是编译器给我以下错误:无法将类型(int8,int8,int8 ....)转换为[CChar]。


阅读 590

收藏
2020-07-07

共1个答案

一尘不染

可能的解决方案:

withUnsafeMutablePointer(&myVarOfStructProduct.name) {
    strlcpy(UnsafeMutablePointer($0), productName, UInt(sizeofValue(myVarOfStructProduct.name)))
}

在该块内部,$0是一个指向元组的(可变)指针。该指针UnsafeMutablePointer<Int8>将按BSD库函数的strlcpy()预期
转换为。

它还使用了这样的事实:Swift字符串productName会自动UnsafePointer<UInt8> 按照 UnsafePointer<UInt8> 函数参数行为的字符串值中的说明进行操作。如该线程的注释中所述,这是通过创建一个临时UInt8数组(或序列?)来完成的。所以, _或者_你可以明确地列举了UTF-8字节,并把它们放到目的地:

withUnsafeMutablePointer(&myVarOfStructProduct.name) {
    tuplePtr -> Void in
    var uint8Ptr = UnsafeMutablePointer<UInt8>(tuplePtr)
    let size = sizeofValue(myVarOfStructProduct.name)
    var idx = 0
    if size == 0 { return } // C array has zero length.
    for u in productName.utf8 {
        if idx == size - 1 { break }
        uint8Ptr[idx++] = u
    }
    uint8Ptr[idx] = 0 // NUL-terminate the C string in the array.
}

另一个可能的解决方案(带有中间NSData对象):

withUnsafeMutablePointer(&myVarOfStructProduct.name) {
    tuplePtr -> Void in
    let tmp = productName + String(UnicodeScalar(0)) // Add NUL-termination
    let data = tmp.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!
    data.getBytes(tuplePtr, length: sizeofValue(myVarOfStructProduct.name))
}

Swift 3更新:

withUnsafeMutablePointer(to: &myVarOfStructProduct.name) {
    $0.withMemoryRebound(to: Int8.self, capacity: MemoryLayout.size(ofValue: myVarOfStructProduct.name)) {
        _ = strlcpy($0, productName, MemoryLayout.size(ofValue: myVarOfStructProduct.name))
    }
}
2020-07-07