C 中的statfs
struct 具有以下char
数组成员:
char f_mntonname[MNAMELEN]; /* directory on which mounted */
char f_mntfromname[MNAMELEN]; /* mounted file system */
模块中这些数组的 Swift 类型Darwin.sys.mount
是一个包含 90 个元素的元组:
public var f_mntonname: (Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8,
...
Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8)
另一个关于将 C char 数组转换为字符串的问题有一个答案,我在这个例子中使用了两次代码:
import Darwin.sys.mount
var vols: UnsafeMutablePointer<statfs>?
let count = getmntinfo(&vols, 0)
if let volsarray = vols, count > 0 {
for i in 0 ..< count {
var vol = volsarray[Int(i)]
let mntOnName = withUnsafePointer(to: &vol.f_mntonname) {
$0.withMemoryRebound(to: UInt8.self,
capacity: MemoryLayout.size(ofValue: vol.f_mntonname)) {
String(cString: $0)
}
}
let mntFromName = withUnsafePointer(to: &vol.f_mntfromname) {
$0.withMemoryRebound(to: UInt8.self,
capacity: MemoryLayout.size(ofValue: vol.f_mntfromname)) {
String(cString: $0)
}
}
print("on \(mntOnName) from \(mntFromName)")
}
}
为了避免重复代码两次,我将转换代码重构为下面的函数,但传递&vol.f_mntonname
给它不会编译,而且我看不到通过对第一个参数使用不同类型来解决问题的方法。
func charArrayToString(_ array: UnsafePointer<Int8>, capacity: Int) -> String {
return array.withMemoryRebound(to: UInt8.self, capacity: capacity) {
String(cString: $0)
}
}
var a = (Int8(65), Int8(66), Int8(67))
print(charArrayToString(&a, capacity: 3)) // doesn't compile
编译器抱怨调用charArrayToString
:
无法将类型“ (Int8, Int8, Int8)
”的值转换为预期的参数类型“ Int8
”
当我通过a
而不是时它也会抱怨&a
:
无法将类型' (Int8, Int8, Int8)
'的值转换为预期的参数类型' UnsafePointer<Int8>
'