Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save blakemerryman/8295f883d20fa91fbce2 to your computer and use it in GitHub Desktop.
Save blakemerryman/8295f883d20fa91fbce2 to your computer and use it in GitHub Desktop.
Adds .insertBytes(...) to NSMutableData in Swift
public extension NSMutableData {
func insertBytes(bytes: UnsafePointer<Void>, withLength length: Int, atIndex index: Int) -> NSMutableData {
if index > self.length {
return NSMutableData()
}
var data = self // Holds self for modification
let lowerRange = NSMakeRange(0, index)
let upperRange = NSMakeRange(index, data.length-index)
let lowerHalfOfData = data.subdataWithRange(lowerRange)
let upperHalfOfData = data.subdataWithRange(upperRange)
data = NSMutableData()
data.appendData(lowerHalfOfData)
data.appendBytes(bytes, length: length)
data.appendData(upperHalfOfData)
return data
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment