Skip to content

Instantly share code, notes, and snippets.

@Ben-G
Last active February 17, 2017 17:24
Show Gist options
  • Save Ben-G/4bc238d243f56a8354d4 to your computer and use it in GitHub Desktop.
Save Ben-G/4bc238d243f56a8354d4 to your computer and use it in GitHub Desktop.
Playing with unsafe Pointers
//: 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