Uso de withMemoryRebound with Apples Swift 3 beta 6
Eu tenho o seguinte problema. Quero converter minha função antiga (trabalhou até o Swift 3 beta 5):
func binarytotype <T> (_ value: [UInt8], _: T.Type) -> T
{
return value.withUnsafeBufferPointer
{
return UnsafePointer<T>($0.baseAddress!).pointee
}
}
Para Swift 3 beta 6 Sintaxe. Esta função converte uma matriz do UInt8 para outro tipo, por exemplo:
let b: [UInt8] = [1,2,3,4,5,6,7,8]
var number: Double = binarytotype(b, Double.self)
Mas, por enquanto, isso não funciona mais no beta 6 e eu tenho que usá-lo com oMemoryRebound, mas realmente não sei como fazê-lo funcionar. Alguém pode me ajudar?
A função inversa disso foi:
func typetobinary <T> (_ value: T) -> [UInt8]
{
var v: T = value
return withUnsafePointer(to: &v)
{
Array(UnsafeBufferPointer(start: UnsafePointer<UInt8>($0), count: MemoryLayout<T>.size))
}
}
Isso também não funciona mais. Mesmo problema. Ambos são necessários para alguns dos meus projetos. Essa função reversa foi chamada como:
var binary: [UInt8] = typetobinary(number)