user965972
user965972

Reputation: 2587

How to append Int to the new Data struct (Swift 3)

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?

Upvotes: 4

Views: 8757

Answers (2)

yycking
yycking

Reputation: 1311

I like to use + or +=

public protocol DataConvertible {
    static func + (lhs: Data, rhs: Self) -> Data
    static func += (lhs: inout Data, rhs: Self)
}

extension DataConvertible {
    public static func + (lhs: Data, rhs: Self) -> Data {
        var value = rhs
        let data = Data(buffer: UnsafeBufferPointer(start: &value, count: 1))
        return lhs + data
    }

    public static func += (lhs: inout Data, rhs: Self) {
        lhs = lhs + rhs
    }
}

extension UInt8 : DataConvertible { }
extension UInt16 : DataConvertible { }
extension UInt32 : DataConvertible { }

extension Int : DataConvertible { }
extension Float : DataConvertible { }
extension Double : DataConvertible { }

extension String : DataConvertible {
    public static func + (lhs: Data, rhs: String) -> Data {
        guard let data = rhs.data(using: .utf8) else { return lhs}
        return lhs + data
    }
}

extension Data : DataConvertible {
    public static func + (lhs: Data, rhs: Data) -> Data {
        var data = Data()
        data.append(lhs)
        data.append(rhs)

        return data
    }
}

sample

var data = Data()
data += 1
data += 1.0
data += UInt8(1)
data += "1"

Upvotes: 4

Code Different
Code Different

Reputation: 93151

Working with pointers is one of my least favorite thing to do in Swift, but it also offer a good learning experience. This works for me:

struct Storage<T: BinaryConvertible>
{
    var data = Data()

    mutating func append(value: T)
    {
        var input = value
        let buffer = UnsafeBufferPointer(start: &input, count: 1)
        self.data.append(buffer)
    }

    func extract(index: Int) -> T
    {
        let startIndex = index * sizeof(T)
        let endIndex = startIndex + sizeof(T)

        var output = T()
        let buffer = UnsafeMutableBufferPointer(start: &output, count: 1)
        let _ = self.data.copyBytes(to: buffer, from: startIndex..<endIndex)

        return output
    }
}

var s = Storage<Double>()
s.append(value: M_PI)
s.append(value: 42)
s.append(value: 100)

print(s.extract(index: 0))
print(s.extract(index: 1))
print(s.extract(index: 2))

Upvotes: 11

Related Questions