Created
October 19, 2018 16:59
-
-
Save T-Pham/dd73fa649ade03d3880ceff2a944d6a4 to your computer and use it in GitHub Desktop.
Swift - in-place mutation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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