IOS development-OC language (5) dictionary, ios-oc

Source: Internet
Author: User

IOS development-OC language (5) dictionary, ios-oc

Dictionary

Key knowledge points:

1. NSDictionary class

2. NSMutableDictionary class

3. Understand the inheritance relationship between NSMutableDictionary and NSDictionary

4. Supplement:

Hashed storage: Also known as hash Storage, is a search technique that tries to establish a ing between the storage location of data elements and key codes. The basic idea of Hash Storage is that the storage address of a node is determined by the key code value of the node. In addition to searching, the Hash technology can also be used for storage.

Hash is a kind of development in array storage. Compared with arrays, the access speed of hash data is higher than that of the array, because the storage location of the data in the array can be found based on some of the content of the stored data, in this way, data access can be quickly realized, and the ideal hash access speed is very fast, unlike the process in the array, some elements in the array are used as the input of the ing function, and the output of the ing function is the location of the stored data. This access speed saves the Implementation of The traversal array, therefore, the time complexity can be considered as O (1), while the time complexity of array traversal is O (n ).

 

============ NSDictionary immutable dictionary ==========================

1. What is a dictionary)

A dictionary is also a collection structure with the same functions as our dictionary tools in reality.

2. What are the dictionary elements?

Any type of object address constitutes a key-Value Pair

3. What is a key-value pair?

Key-value pairs are composed of keys and values which must correspond one to one.

The key in the key-value pair must be unique.

4. dictionary storage

A dictionary is a collection of unordered storage.

5. How to find the value

Find the corresponding value based on the key in the key-Value Pair

 

 

========================================================== ==========

1. NSDictionary (immutable) Creation

 

1) how to create a dictionary object

(1) Create an empty dictionary (not required)

NSDictionary * dic = [[NSDictionary alloc] init];

Dic = [NSDictionary dictionary];

(2) create a dictionary with objects

 

/*

1. A key-value Pair)

@ "Key1" = @ "1111"

*/

Dic = [NSDictionary dictionaryWithObject: @ "1111" forKey: @ "key1"];

/*

2. There are multiple key-value pairs

*/

NSArray * objArray = @ [@ "value1", @ "value2"];

NSArray * keyArray = @ [@ "key1", @ "key2"];

Dic = [NSDictionary dictionaryWithObjects: objArray forKeys: keyArray];

/*

3. There are multiple key-value pairs (also written)

*/

Dic = [NSDictionary dictionaryWithObjectsAndKeys: @ "value1", @ "key1", @ "value2", @ "key2", @ "value3", @ "key3", nil];

NSLog (@ "% @", dic );

/*

4. Copy the key-value pair of the specified dictionary and return a new dictionary.

*/

[NSDictionary dictionaryWithDictionary: <# (NSDictionary *) #>];

/*

5. Quick Creation

Formula (key-value pairs are separated ):

@ {Key: value, key: value ...}

*/

Dic =@{}; // create an empty dictionary

Dic =@{@ "key1": @ "value", @ "key2": @ "value2", @ "key6": @ "value6 "};

 

2. Obtain the number of key-value pairs

[Dic count]

 

3. How to obtain the corresponding value through the key

(1) [dic objectForKey: @ "key"]; Dictionary Method

Id obj = [dic valueForKey: @ "key3"]; Dictionary Method

[Arr objectAtIndex: index]; Array Method

(2) obj = dic [@ "key2"];

Note: A [B] If B is an integer and A is an array,

If B is A string, A is A dictionary.

 

4. Obtain all keys in the dictionary.

[Dic allKeys]

 

5. Obtain all values in the dictionary.

[Dic allValues]

 

 

6. Write the dictionary to the specified file.

Dic writeToFile: path atomically: YES];

 

 

7. Read Dictionary data from the file

[NSDictionary dictionaryWithContentsOfFile: path];

 

8. Traverse dictionary content

1) enumeration method:

<1> key traversal:

KeyEnumerator message

Purpose: obtain all key values.

NSEnumerator * emunerator = [dict keyEnumerator];

Id obj;

While (obj = [emunerator nextObject]) {

NSLog (@ "% @", obj );

}

 

<2> worth traversing

ObjectEnumerator message

 

2) Quick enumeration: The Key is obtained.

For (id key in dict)

{

NSLog (@ "key-% @, value = % @, key, [dict objectForKey: obj]); // get the key and get the value.

}

 

 

 

9. Comparison between NSDictionary and NSArray

1) Advantages of dictionary relative to array

The element retrieval speed of the dictionary is faster than that of the array because the dictionary obtains the specific position of each element through the hash algorithm.

2) how to obtain elements

Array uses subscript to obtain the content objectAtIndex of a specified Element

The dictionary uses the key to obtain the content objectForKey of the specified element.

 

 

 

============ NSMutableDictionary variable dictionary ============================

 

1. Differences between NSMutableDictionary and NSDictionary

2. How to create

// Initialize an empty dictionary

NSMutableDictionary * dic = [NSMutableDictionary dictionary];

Dic = [[NSMutableDictionary alloc] init];

 

3. Add a key-Value Pair

/*

(1) The key is unique and the value can be the same.

(2) When a key-value pair is added, if the key exists, the original value is replaced ). If the key does not exist, add a key-Value Pair (ADD ).

*/

(1) [dic setObject: @ "value1" forKey: @ "key1"];

 

Note: No key exists or the key exists.

If the program does not exist, it will fail to be saved, and the program will crash. Otherwise, it will overwrite

4. Add the content of another dictionary to the current dictionary.

/*

Add the key-value pairs in dic2 to dic1

[A addEntriesFromDictionary: B] Add the key-value pairs in B to Dictionary A. If the key in B has the same key in A, the value will be replaced.

*/

[Dic1 addEntriesFromDictionary: dic2];

 

[Dic1 setValuesForKeysWithDictionary: dic3];

 

 

5. Deletion of variable dictionaries

1) delete a group of key-value pairs by specifying the key

[Dic1 removeObjectForKey: @ "key1"];

 

2) You can specify multiple keys to delete related key-value pairs.

[Dic1 removeObjectsForKeys: @ [@ "key1", @ "key2", @ "key4"];

 

3) delete all key-value pairs in the dictionary

[Dic1 removeAllObjects];

6. Modify

1) modify the entire dictionary

[Dic1 setDictionary: dic3];

2) modify key-value pairs

[Dic1 setObject: <# (id) #> forKey: @ "dictionary key"];

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.