Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.