With NSMutableData
I could create an array of Int's
or Float
's and store those to disk.
protocol BinaryConvertible
{
init()
}
extension Int : BinaryConvertible {}
struct Storage<T: BinaryConvertible>
{
let data = NSMutableData()
func append(value: T)
{
var input = value
data.append(&input, length: sizeof(T))
}
func extract(index: Int) -> T
{
var output = T()
let range = NSRange(location: index * sizeof(T), length: sizeof(T))
data.getBytes(&output, range: range)
return output
}
}
Swift 3 has a new Data
type which uses NSData
under the hood. Like String
and NSString
. I can't figure out how to add e.g. a Double
using the new methods.
The append function now expects a UnsafePointer<UInt8>
, but how do you create this from a Double
or any random struct for that matter?