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?

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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.
Can you confirm no longer behaves this way?
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.