Skip to content

Instantly share code, notes, and snippets.

@DineshKachhot
Created May 28, 2019 12:54
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 DineshKachhot/a5216ba3677fb83e736c15d43986a66c to your computer and use it in GitHub Desktop.
Save DineshKachhot/a5216ba3677fb83e736c15d43986a66c to your computer and use it in GitHub Desktop.
Check memory address of Value type and Reference type in Swift 5
// Get memory address of value type
func address(of value: UnsafeRawPointer) -> Int {
return unsafeBitCast(value, to: Int.self)
}
var num1 = 55
var num2 = 55
print(NSString(format: "%p", address(of:&num1))) // -> "0x11fc3c5a0"
print(NSString(format: "%p", address(of:&num2))) // -> "0x11fc3c5a8"
num2 = 77
print(NSString(format: "%p", address(of:&num2))) // -> "0x11fc3c5a8"
// Get memory address of referance
func address<T: AnyObject>(of referance: T) -> Int {
return unsafeBitCast(referance, to: Int.self)
}
class Test {}
var o = Test()
var p = Test()
print(NSString(format: "%p", address(of: o))) // -> 0x6000018c0510
print(NSString(format: "%p", address(of: p))) // -> 0x6000018c8410
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment