Created
June 27, 2014 14:18
-
-
Save roop/92bac4f644d2259d91fd to your computer and use it in GitHub Desktop.
This file contains 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
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