Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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