Last active
August 29, 2015 14:07
-
-
Save blakemerryman/8295f883d20fa91fbce2 to your computer and use it in GitHub Desktop.
Adds .insertBytes(...) to NSMutableData in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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