07. How to print the memory address of a variable?

It will take about 1 minutes to finish reading this article.

We can print the memory address of a variable like this:

1
2
3
var a = 100
withUnsafePointer(to: &a) {ptr in print(ptr)}
//result: 0x00007ff7bfeff210

Or using the following method is OK:

1
2
3
4
5
6
func printPointer<T>(ptr: UnsafePointer<T>) {
print(ptr)
}
var a = 100
printPointer(ptr: &a)
//result: 0x00007ff7bfeff220