Is there anyway to simulate the [NSString stringWithFormat:@"%p", myVar]
, from Objective-C, in the new Swift language?
For example:
let str = "A String" println(" str value \(str) has address: ?")
To get the (stack) address of a struct, build-in type or object reference. In Objective-C this would be [NSString stringWithFormat:@"%p", &o] or [NSString stringWithFormat:@"%p", &i] . s is struct. So if s is assigned to another variable s2 , the value will be copied and the returned address for s2 will be different.
To print the memory address, we use '%p' format specifier in C. To print the address of a variable, we use "%p" specifier in C programming language.
Note: This is for reference types.
print(Unmanaged.passUnretained(someVar).toOpaque())
Prints the memory address of someVar. (thanks to @Ying)
print(Unmanaged<AnyObject>.passUnretained(someVar as AnyObject).toOpaque())
Prints the memory address of someVar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With