Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the 'hash' method do in Objective-C

Occasionally, when I am looking down the list of selectors that a object responds to, I see hash come up. For a while now, I have wondered what it meant, but I never came around to finding out.

I would really appreciate it if you could tell me what the selector does and common uses for doing so.

like image 732
max_ Avatar asked Oct 01 '11 16:10

max_


People also ask

What is the purpose of hash include?

In this scenario, hash codes generate an index to store values. So, here, hashing is used to index and retrieve information from a database because it helps accelerate the process; it is much easier to find an item using its shorter hashed key than its original value.

What is the hash value of an object?

The hash() method returns the hash value of an object if it has one. Hash values are just integers that are used to compare dictionary keys during a dictionary look quickly.

What does hashed data mean?

Hashed data maps the original string of characters to data of a fixed length.

What is hash function in sql?

Hashing technique is used to calculate the direct location of a data record on the disk without using index structure. In this technique, data is stored at the data blocks whose address is generated by using the hashing function. The memory location where these records are stored is known as data bucket or data blocks.


1 Answers

It computes a hash of the object, which is especially useful in HashTables like when the object is used as a key for an NSDictionary.

An object hash has to have the following properties:

  • Two objects of the same class that are to be considered equal should return the same hash
  • Two objects with a diffent hash will never be considered as equal
  • Two objects with the same hash are not necessary equal to each other; anyway, the more unique the hash is, the better the performance for NSDictionary lookup will be.
  • If should also be computable as fast as possible to avoid performance issues in dictionaries

For more info, read the NSObject Protocol documentation where this hash method is defined.


For example the hash function for a string could be the number of characters of this string, or the sum of the ascii codes of its character, or anything similar.

When you search for a given key in an NSDictionary, one solution would be to compare the searched key with all other keys in the dictionary, which would need to loop thru all the keys and call isEqual on each key, and that will take a long time if the dictionary have a lot of entries. So instead, Cocoa will compute the hash of the searched key, and compare it with all the (pre-computed) hashes of the keys in the dictionary.

This is much more efficient because it will only need to make comparisons between NSUInteger values, even if the keys of the NSDictionary are NSStrings or other objects, so this is really faster. Once it has found the keys that have the same hash as the hash of the searched key, it can then loop thru all the keys with this hash and compare them with the searched key by calling isEqual, but at this stage there will be much less keys to loop thru (and even possibly only one if the hash of the key is really unique)

like image 54
AliSoftware Avatar answered Oct 02 '22 19:10

AliSoftware