Skip to content

Instantly share code, notes, and snippets.

@mikeash
Created June 29, 2016 17:03
Show Gist options
  • Save mikeash/687e377f2b08f8f10d48cef1af122076 to your computer and use it in GitHub Desktop.
Save mikeash/687e377f2b08f8f10d48cef1af122076 to your computer and use it in GitHub Desktop.
import Darwin
struct S {
var whatever: AnyObject? = nil
mutating func stuff() {
withUnsafePointer(&whatever, {
for offset in 0.stride(to: 128, by: sizeof(AnyObject.self)) {
let ptr = UnsafePointer<UInt8>($0) - offset
if malloc_size(ptr) > 0 {
print(ptr)
let obj = unsafeBitCast(ptr, AnyObject.self)
print("I'm in an object of type \(obj.dynamicType)")
break
}
}
})
}
}
class C {
var a = 42
var b = 1.1
var s = S()
}
let c = C()
c.s.stuff()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment