Skip to content

Instantly share code, notes, and snippets.

@T-Pham
Created October 19, 2018 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save T-Pham/dd73fa649ade03d3880ceff2a944d6a4 to your computer and use it in GitHub Desktop.
Save T-Pham/dd73fa649ade03d3880ceff2a944d6a4 to your computer and use it in GitHub Desktop.
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