Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Swift - in-place mutation
func address(o: UnsafeRawPointer) -> Int {
return Int(bitPattern: o)
}
func addressHeap<T: AnyObject>(o: T) -> Int {
return unsafeBitCast(o, to: Int.self)
}
////////////////////////////////////////////
struct Point {
var x: Float
var y: Float
}
struct Size {
var width: Float
var height: Float
}
struct Rect {
let origin: Point
var size: Size
}
////////////////////////////////////////////
var frame = Rect(origin: Point(x: 0, y: 0), size: Size(width: 10, height: 10)) {
didSet {
print("Frame changed! \(frame)")
}
}
print(address(o: &frame.size))
frame.size.height = 600
print(address(o: &frame.size))
//Should print the same address twice, before and after changing height
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.