Reputation: 303
I want to get the memory address of a value type of constant in Swift. For example with the variables it looks like:
My code:
var value = 10
withUnsafePointer(to: &value) {
print(" str value \(value) has address: \($0)")
}
Upvotes: 1
Views: 1030
Reputation: 54785
There is a variant of withUnsafePointer(to:,_:)
that accepts a non-inout argument as its first input argument.
let immutableValue = 1
withUnsafePointer(to: immutableValue, { pointer -> Void in
print(pointer)
})
As MartinR pointed out, this pointer though is valid only for the invocation of the closure.
It is also important to note that with values known at compile time, the compiler might substitute the value in place of the variable, so the variable might not even exist in memory at runtime.
Upvotes: 3