7

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.

enter image description here

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?

Kyokook Hwang
  • 2,682
  • 21
  • 36

1 Answers1

1

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).

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I posted it about 5 years ago and updated it few hours ago in order to fix some sentences grammatically incorrect. Thank you for your answer. it will be helpful for someone who wanna see. – Kyokook Hwang Sep 22 '20 at 07:11
  • Can you confirm no longer behaves this way? – matt Sep 22 '20 at 11:15
  • Technically, I can't confirm that it doesn't behave anymore in the way i tested before since `unsafeAddressOf` is removed. anyways, i tested it with `UnsafePointer(arr)` and observed that it looked to work in a way i expected – Kyokook Hwang Sep 23 '20 at 07:21