Print the variable memory address in SWIFT

Is there anyway to simulate the [NSString stringWithFormat:@”%p”,myVar] code with the new swift language?

For example:

let str = "A String"
println(" str value \(str) has address:? ")

Swift 2

Now this is the standard Part of the library: unsafeAddressOf.

/// Return an UnsafePointer to the storage used for `object`. There's
/// not much you can do with this other than use it to identify the
/// object

Swift 3

For Swift 3, use withUnsafePointer:

var str = "A String"
withUnsafePointer(to: &str) {
print(" str value \(str) has address: \($0)")
}< /pre>

Anyway, use the new swift language to simulate the [NSString stringWithFormat:@"%p",myVar] code?

For example:

let str = "A String"
println(" str value \(str) has address:? ")

Swift 2

Now this is part of the standard library: unsafeAddressOf.

/// Return an UnsafePointer to the storage used for `object`. There's
/// not much you can do with this other than use it to identify the
/ // object

Swift 3

For Swift 3, use withUnsafePointer:

var str = "A String"< br />withUnsafePointer(to: &str) {
print(" str value \(str) has address: \($0)")
}

Leave a Comment

Your email address will not be published.