Application Analysis of Hash in iOS

Application Analysis of Hash in iOS, AssociatedObjec 해시게임

① The data storage structure (Hash table + Hash table) used by the underlying implementation of the associated object


The associated object uses the structure of HashMap nested HashMap to store data.

In simple terms, it is to take out the second HashMap that stores all associated objects of the object from the first HashMap according to the object,

and then take it out from the second HashMap according to the attribute name.

The value and policy correspond to the attribute.


The original intention of designing an associated object: By passing in the object + attribute name,

the attribute value can be found.

After the solution design is implemented,

the basic steps to find the associated object of an object:

Known condition 1: object, so the first HashMap (AssociationsHashMap) is introduced,

a value that can uniquely represent the object is used as the key,

and the structure (name: value + strategy) of all associated objects that store the object is used as the value;


Known condition 2: attribute name, so lead to the second HashMap (ObjectAssociationMap),

use the attribute name as the key,

and use the structure (value + strategy) corresponding to the attribute name as the value.

② The data storage structure used by the weak underlying implementation (Hash table + array)

Weak uses the structure of a global HashMap nested array to store data.

When destroying the object (the object pointed to by the weak pointer),

find the array storing all weak pointers to the object from the HashMap according to the object,

and then put the array in the array.

All elements are set to nil.
The biggest feature of weak is that when the object is destroyed,

it is automatically set to nil to reduce the risk of accessing wild pointers,

which is also the original intention of designing weak.


After the solution is designed and implemented,

the basic steps for setting the weak pointer to nil: When the object is deallocated,

from the global HashMap, according to a value that uniquely represents the object as the key,

find the array that stores all weak pointers to the object;


Sets all elements in the array to nil.

③ The data storage structure used by the underlying implementation of KVO (Hash table + array)

An object can be observed by n objects, and n properties of an object can be observed by n objects respectively.

④ The principle of iOS App signature (MD5 + hash table + asymmetric encryption RSA)

Consistent hash algorithm + asymmetric encryption and decryption algorithm:

Digital signature: When transferring data,

the original data and digital signature will be sent together.

After the other party gets the data, the Hash value of the original data is obtained through the same Hash algorithm,

and then the verification Hash in the digital signature is performed through asymmetric encryption.

The value is decrypted, and finally, the two Hash values ​​are compared to see if they are consistent.


Code Signing: Code signing is the digital signature of an executable file or script to confirm that the software has not been modified or damaged after being signed.

Its principle is similar to that of a digital signature,

except that the signature is not data, but code.


Can you really see from the above structure that NSMapTable is a “hash table + linked list” data structure?

Seek time = O(1) + O(m) to insert or delete an object in NSMapTbale,

where m is the worst possible n:


O(1): Hash the key to getting the location of the bucket;


O(m): The time for different keys to get the same hash value and store them in the linked list,

and traverse the linked list.


Do the above conclusion and corresponding explanation seem reasonable?

Below we continue the analysis.


_CFDictionary package.


– NSDictionary is an encapsulation of _CFDictionary,

and the open-addressing linear detection method is used to resolve hash collisions.