Do you need help on a specific subject? Use the contact form (Request a blog entry) on the right hand side.

2016-08-02

Missing "remove" operation on Data in Swift 3 | An Extension for the Data type in Swift 3

It could be me, but is anyone missing the "remove" operation on the "Data" type in Swift 3?

Well, there is an extension for that:

extension Data {

    mutating func removeSubrange(_ range: Range<Int>) {
        var dummy: UInt8 = 0
        let empty = UnsafeBufferPointer<UInt8>(start: &dummy, count: 0)
        self.replaceSubrange(range, with: empty)
    }
}

Example usage:

var buffer = Data(bytes: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
let range = Range(uncheckedBounds: (lower: 1, upper: 5))
buffer.removeSubrange(range)

Output: <00050607 0809>

Happy coding...

Did this help?, then please help out a small independent.
If you decide that you want to make a small donation, you can do so by clicking this
link: a cup of coffee ($2) or use the popup on the right hand side for different amounts.
Payments will be processed by PayPal, receiver will be sales at balancingrock dot nl
Bitcoins will be gladly accepted at: 1GacSREBxPy1yskLMc9de2nofNv2SNdwqH

We don't get the world we wish for... we get the world we pay for.

No comments:

Post a Comment