Recently, I found different bridging between Array and Dictionary. In the following code, I tried to check an address of instances being created by implicitly bridging of Array and Dictionary.
As you can see, dic
is passed into unsafeAddressOf
function which prints an address of an instance of AnyObject
passed. It's expected to be converted to NSDictionary when it passed to the function since dic
is a value of Dictionary. As you can easily see, this bridging made new instance of NSDictionary. Based on the result, I presumed that calling the function twice would result in creating two instances of NSDictionary. But, same addresses got printed. It seems that just one instance has been made.
The experiment with NSArray looks work well in my assumption.
Why do they work differently?
Arrays are ordered collections of values. Sets are unordered collections of unique values. Dictionaries are unordered collections of key-value associations. Arrays, sets, and dictionaries in Swift are always clear about the types of values and keys that they can store.
moving or capable of moving with great speed or velocity; fleet; rapid: a swift ship. coming, happening, or performed quickly or without delay: a swift decision.
Can't reproduce. The modern equivalents of unsafeAddressOf
are
Unmanaged.passUnretained(arr).toOpaque()
and
ObjectIdentifier(arr)
and they both yield the same consistent address per object (which needs to be bridged explicitly to AnyObject).
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