Last active
February 17, 2017 17:24
-
-
Save Ben-G/4bc238d243f56a8354d4 to your computer and use it in GitHub Desktop.
Playing with unsafe Pointers
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
//: Playground - noun: a place where people can play | |
import Foundation | |
let arr = [1,5,7,8] | |
let pointer = UnsafeMutablePointer<[Int]>.alloc(4) | |
pointer.initialize(arr) | |
let x = pointer.memory[3] | |
println(x) | |
pointer.destroy() | |
pointer.dealloc(4) | |
class A { | |
var x: String? | |
convenience init (_ x: String) { | |
self.init() | |
self.x = x | |
} | |
func description() -> String { | |
return x ?? "" | |
} | |
} | |
let arr2 = [A("OK"), A("OK 2")] | |
let pointer2 = UnsafeMutablePointer<[A]>.alloc(2) | |
pointer2.initialize(arr2) | |
pointer2.memory | |
let y = pointer2.memory[1] | |
println(y) | |
pointer2.destroy() | |
pointer2.dealloc(2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment