There is something I am not understanding about how Swift manages memory address of String(s)
1. Reference types
Here foo and boo are 2 pointers to the same memory location.
class Foo { }
let foo = Foo()
let boo = foo
unsafeAddressOf(foo) // "UnsafePointer(0x7FCD13719BE0)"
unsafeAddressOf(boo) // "UnsafePointer(0x7FCD13719BE0)"
Good.
2. Value types
let word0 = "hello"
let word1 = word0
Now word0 and word1 are value types but here the copy on write mechanism is involved.
[...] However, Swift only performs an actual copy behind the scenes when it is absolutely necessary to do so. Swift manages all value copying to ensure optimal performance, and you should not avoid assignment to try to preempt this optimization. https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html#//apple_ref/doc/uid/TP40014097-CH13-XID_134
So why do they have 2 different memory addresses?
unsafeAddressOf(word0) // "UnsafePointer(0x7FCD1342ACE0)"
unsafeAddressOf(word1) // "UnsafePointer(0x7FCD13414260)"
3. More
Also please note that String is a struct that somehow conforms to AnyObject.
Tested with Xcode 7 GM Playground and Swift 2.0.
func unsafeAddressOf(object: AnyObject) -> UnsafePointer<Void>
takes an AnyObject parameter, i.e. an instance of a class.
It returns the pointer to the storage used for the object referenced
by object.
addressOf() cannot be used with struct variables:
struct Foo { }
var f = Foo()
let a = unsafeAddressOf(f)
// error: cannot invoke 'unsafeAddressOf' with an argument list of type '(Foo)'
String is a struct, however, it is automatically bridged to NSString when passed to a function expecting an object. So
let word0 = "hello"
let p1 = unsafeAddressOf(word0)
actually executes
let p1 = unsafeAddressOf(word0 as NSString)
You get not the address of the word0 variable, but the pointer to the
memory location of the bridged NSString object.
It seems that you cannot make any assumptions on whether this bridging
returns the identical NSString object (or more generally, the same
Foundation object) when done repeatedly on the same Swift string. In a Playground, even
let word0 = "hello"
let p1 = unsafeAddressOf(word0)
let p2 = unsafeAddressOf(word0)
let p3 = unsafeAddressOf(word0)
returns three different addresses (but the same addresses in a compiled project). The same observation (for arrays and dictionaries) was made in A different bridging between Array and Dictionary.
Swift 3.0 Unmanaged.passUnretained(object).toOpaque()
来源:https://stackoverflow.com/questions/32638879/swift-strings-and-memory-addresses