我有以下问题.我想转换我的旧函数(直到Swift 3 beta 5):
func binarytotype <T> (_ value: [UInt8], _: T.Type) -> T
{
return value.withUnsafeBufferPointer
{
return UnsafePointer<T>($0.baseAddress!).pointee
}
}
Run Code Online (Sandbox Code Playgroud)
转到Swift 3 beta 6语法.此函数将UInt8的数组转换为另一种类型,例如:
let b: [UInt8] = [1,2,3,4,5,6,7,8]
var number: Double = binarytotype(b, Double.self)
Run Code Online (Sandbox Code Playgroud)
但是现在这在beta 6中不再起作用了,我必须使用withMemoryRebound,但我真的不知道,如何让它运行.有谁能够帮我?
相反的功能是:
func typetobinary <T> (_ value: T) -> [UInt8]
{
var v: T = value
return withUnsafePointer(to: &v)
{
Array(UnsafeBufferPointer(start: UnsafePointer<UInt8>($0), count: MemoryLayout<T>.size))
}
}
Run Code Online (Sandbox Code Playgroud)
这也不再适用.同样的问题.我的一些项目都需要这两个项目.这个反向函数被称为:
var binary: [UInt8] = typetobinary(number)
Run Code Online (Sandbox Code Playgroud) 我需要一个函数,比如旧的“getch()”,在 Objective C 或 Swift 中,从键盘读取一个字符而没有回声,并且没有 nedd 在输入字符后按回车键使函数继续。
我知道,这仅在对命令行工具进行编程时才有趣,也许是为了进行选择或编写编辑器。