Created
January 11, 2017 19:35
-
-
Save mgadda/026854c877fb79631225e8da08245199 to your computer and use it in GitHub Desktop.
How to write to an immutable array
This file contains hidden or 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
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