Uso de withMemoryRebound con Apples Swift 3 beta 6
Tengo el siguiente problema. Quiero convertir mi antigua función (funcionó hasta 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 Sintaxis. Esta función convierte una matriz de UInt8 a otro tipo, por ejemplo:
let b: [UInt8] = [1,2,3,4,5,6,7,8]
var number: Double = binarytotype(b, Double.self)
Pero por ahora esto ya no funciona en beta 6 y tengo que usarlo con MemoryRebound, pero realmente no sé cómo hacerlo funcionar. Alguien puede ayudarme?
La función inversa de esto era:
func typetobinary <T> (_ value: T) -> [UInt8]
{
var v: T = value
return withUnsafePointer(to: &v)
{
Array(UnsafeBufferPointer(start: UnsafePointer<UInt8>($0), count: MemoryLayout<T>.size))
}
}
Esto ya no funciona más. El mismo problema. Ambos son necesarios para algunos de mis proyectos. Esta función inversa fue llamada como:
var binary: [UInt8] = typetobinary(number)