Swift3 - 如何将UnsafeMutableBufferPointer <cchar>转换为UnsafeMutablePointer <utsname>

时间:2017-02-06 14:11:29

标签: swift swift3

我正在将Swift 2.x项目迁移到Swift 3。

我在使用Swift 3语法将UnsafeMutableBufferPointer<CChar>转换为UnsafeMutablePointer<utsname>时遇到问题。

Swift 2.x

(ptr: inout UnsafeMutableBufferPointer<CChar>) -> DeviceTypes in
        uname(UnsafeMutablePointer<utsname>(ptr.baseAddress))

1 个答案:

答案 0 :(得分:1)

斯威夫特3:

(ptr: inout UnsafeMutableBufferPointer<CChar>) -> DeviceTypes in

        guard let pointer = ptr.baseAddress?.withMemoryRebound(to: utsname.self, capacity: 1, { ptr in
            return ptr
        }) else {
            return DeviceTypes.unrecognized
        }
        uname(pointer)