Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A different bridging between Array and Dictionary

Tags:

swift

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?

like image 890
Kyokook Hwang Avatar asked Jul 08 '15 16:07

Kyokook Hwang


People also ask

How is a dictionary different from an array?

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.

What is a dictionary Swift?

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.


1 Answers

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

like image 198
matt Avatar answered Oct 01 '22 20:10

matt