Skip to content

Instantly share code, notes, and snippets.

@roop
Created June 27, 2014 14:18
Show Gist options
  • Save roop/92bac4f644d2259d91fd to your computer and use it in GitHub Desktop.
Save roop/92bac4f644d2259d91fd to your computer and use it in GitHub Desktop.
extension Array {
var unsafePointerToElements: UnsafePointer<T> {
return self.withUnsafePointerToElements { return $0 }
}
subscript (index: Int) -> T {
get {
let ptr = self.unsafePointerToElements
return ptr[index]
}
set(rhs) {
self.unshare()
let ptr = self.unsafePointerToElements
if (ptr) {
ptr[index] = rhs
}
}
}
}
var a1: Array<Int> = [40, 41]
var a2 = a1
a2[1] = 42
println(a1) // [40, 41]
println(a2) // [40, 42]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment