iOS有些API是调用C函数,Swift端获得的数据是CChar元祖,需要转成String方便使用,下面的代码以获取手机型号为例
方式一
var systemInfo = utsname()
uname(&systemInfo)
let deviceModel = withUnsafePointer(to: systemInfo.machine) { tuplePointer inlet cCharPointer = unsafeBitCast(tuplePointer, to: UnsafePointer<CChar>.self)let machine = String(utf8String: cCharPointer)!print("machine: \(machine)")return machine
}
方式二
var systemInfo = utsname()
uname(&systemInfo)let mirror = Mirror(reflecting: systemInfo.machine)
let arr = mirror.children.map { $0.value as! CChar }
let machine = String(cString: arr)