Assuming that data
has type Data
, the following should work:
let tempData: NSMutableData = NSMutableData(length: 26)!
data.withUnsafeBytes {
tempData.replaceBytes(in: NSMakeRange(0, data.count), withBytes: $0)
}
using the
/// Access the bytes in the data.
///
/// - warning: The byte pointer argument should not be stored and used outside of the lifetime of the call to the closure.
public func withUnsafeBytes<ResultType, ContentType>(_ body: @noescape (UnsafePointer<ContentType>) throws -> ResultType) rethrows -> ResultType
method of Data
. Inside the closure $0
is a UnsafePointer<Void>
to the bytes (UnsafeRawPointer
in Xcode 8 beta 6).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…