Skip to content

Instantly share code, notes, and snippets.

@mgadda
Created January 11, 2017 19:35
Show Gist options
  • Save mgadda/026854c877fb79631225e8da08245199 to your computer and use it in GitHub Desktop.
Save mgadda/026854c877fb79631225e8da08245199 to your computer and use it in GitHub Desktop.
How to write to an immutable array
let immutable = Array<Int32>(repeating: 0, count: 4) // => [0, 0, 0, 0]
let ptr = UnsafeMutablePointer<Int32>(mutating: immutable)
let mutableBuffer = UnsafeMutableBufferPointer(start: ptr, count: immutable.count * MemoryLayout<Int32>.size)
let data: Data = Data(bytes: [
0, 0, 0, 0,
1, 0, 0, 0,
2, 0, 0, 0,
3, 0, 0, 0])
data.copyBytes(to: mutableBuffer)
immutable // => [0, 1, 2, 3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment