With NSMutableDataI could create an array Int'sor Floatand save them 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 type Datathat uses NSDataunder the hood. As Stringwell as NSString. I canβt figure out how to add, for example. a Doubleusing new methods.
The append function now expects UnsafePointer<UInt8>, but how do you create it from Doubleor any random structure?
source
share