Created
May 28, 2019 12:54
-
-
Save DineshKachhot/a5216ba3677fb83e736c15d43986a66c to your computer and use it in GitHub Desktop.
Check memory address of Value type and Reference type in Swift 5
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
// 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