Dictionary Class (11)

Source: Internet
Author: User
Tags allkeys

Dictionary class

Key-value pairs: Key-value

A key-value pair, in a metaphor, is a bunch of messy, disorderly goods, how to find a cargo, and take out the goods. So here we have to label the goods, so that in this pile of goods, through the label, to find the goods they want.

The key-value pair is similar to the existence, we use a key to save the name, with value to save the actual value, so you can directly through this key to fetch the value of the values.

Key is equivalent to the label of the goods and value equals the actual goods.

Note: In a bunch of data, key cannot be duplicated.

Immutable dictionary nsdictionary:

A dictionary is a container used to hold a collection of data that has key-value such a relationship. In the dictionary, the value of key is written in OC and can be duplicated, but the same key is saved only the first one when the object is stored. (Strictly speaking, the value of key cannot be duplicated.) Once created, Nsdictionary cannot be modified, cannot be added, cannot be deleted, and can only read value by key.

The order in which the dictionaries are stored is not stored according to the wishes of the individual. Instead, the value is calculated according to the hash algorithm, and then stored.

Creation of dictionaries: Initwithobjectsandkeys

April 17, 2015 11:16:18 Beijing

Dictionary

Key-value pairs: (key-value)

Key (Key): the equivalent of the label affixed to the goods

Value: the equivalent of the goods

Find goods by tag is equivalent to finding value by key

Nsdictionary (Immutable dictionary)

Value can be repeated

Key cannot be duplicated (can be repeated, but cannot be stored in a dictionary)

stored in the order of the values calculated from the hash (hash).

Nsdictionary *dict1 = [[Nsdictionary alloc]initwithobjectsandkeys:@ ' Value_hello world ', @ ' key_a ', @ ' value_this is A Place put value ", @" Key_b ", @" Value_biger then bigger ", @" B ", @" CCC ", @" C ", nil];//with nil as end flag

NSLog (@ "%@", dict1);/*

2015-04-17 11:30:36.976 oclesson5_ Dictionary [1168:55,441] {

B = "Value_biger then bigger";

C = CCC;

"Key_a" = "Value_hello World";

"Key_b" = "Value_this is a place put value";

}

*/

A:1,b:2,c:3,d:4

Nsdictionary *dict2 = [nsdictionary dictionarywithobjectsandkeys:@ "1", @ "A", @ "2", @ "B", @ "3", @ "C", @ "4", @ "D", nil];

NSLog (@ "%@", dict2);/*

2015-04-17 11:35:28.484 oclesson5_ Dictionary [1199:56,836] {

A = 1;

B = 2;

C = 3;

D = 4;

}

*/

Initwithobjectsandkeys This method is one of the methods of initializing a dictionary that, when initialized, presses a pair of accesses, the front is value, and the key is behind. Use nil as the end sign.

Create a dictionary of two arrays: Dictionarywithobjects Forkeys

This is actually called the convenience constructor. Dictionarywithobjects This method, the parameters are two, the front parameter is the value of the group, the argument behind is the array of key.

Create a dictionary of two arrays

Nsarray *valuearr = [Nsarray arraywithobjects:@ "1", @ "2", @ "3", @ "4", nil];

Nsarray *keyarr = [Nsarray arraywithobjects:@ "A", @ "B", @ "C", @ "D", nil];

Nsdictionary *dict3 = [nsdictionary dictionarywithobjects:valuearr Forkeys:keyarr];

NSLog (@ "%@", dict3);/*

2015-04-17 11:42:01.087 oclesson5_ Dictionary [1238:58,650] {

A = 1;

B = 2;

C = 3;

D = 4;

}

*/

Value by key:

        //Two arrays Create a dictionary

        Nsarray *valuearr = [Nsarray arraywithobjects:@ "1", @ "2", @ "3", @ "4", nil];

        nsarray *keyarr = [Nsarray arraywithobjects:@ "A", @ "B", @ " C ", @" D ", nil];

        nsdictionary *dict3 = [nsdictionary Dictionarywithobjects:valuearr Forkeys:keyarr];

       //Key value               (value of key)

        NSString *s1 = [dict3 objectforkey:@ "C"];

        NSLog (@ "%@", S1);//2015-04-17 11:44:54.895 oclesson5_ dictionary [1254:59,540] 3

Count: Calculate the number of key-value pairs

Count says the number of key-value pairs, not the number of keys, and the number of value.

Nsuinteger u1 = [Dict3 count];

NSLog (@ "%lu", U1);//2015-04-17 11:46:50.807 oclesson5_ dictionary [1268:60,069] 4

Get all keys, get all value

Get all keys, return an array, inside is all key

Nsarray *keysarr = [Dict3 AllKeys];

Get all the value, return an array, inside is all the value

Nsarray *valuesarr = [Dict3 allvalues];

NSLog (@ "%@", Keysarr),/*2015-04-17 11:51:31.940 oclesson5_ dictionary [1289:61531] (A,

D

B

C

)*/

NSLog (@ "%@", Valuesarr),/*2015-04-17 11:51:31.940 oclesson5_ dictionary [1289:61,531] (

1,

4,

2,

3

)

*/

Attention:

1, the dictionary and the array, cannot save scalar (basic data type data), can only pass the basic data type to the object through the NSNumber, can save in the dictionary.

2. All keys in the dictionary cannot be duplicated.

3. The key of the dictionary is the ID type (any type (provided that the Nscopying protocol is adhered to)) and value is the ID type.

4, abide by the nscopying agreement

5, do not consider the problem of storage order.

An exercise:

Print values for all value: (1, allvalues 2, AllKeys + Objectforkey)

       // Exercise: Dict3 Print all value values (2 ways)

        nsarray *temp1 = [dict3 Allvalues];

        for (int i = 0; i < [Temp1 count]; i++) {

            NSLog (@ "%@", Temp1[i]);

       }

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;

        nsarray *temp2 = [Dict3 AllKeys];

        for (int i = 0; i < [Temp2 count]; i++) {

            NSLog (@ "%@", [Dict3 Objectforkey:temp2[i]]);

       }

Variable dictionary nsmutabledictionary

Initialize with Init

Nsmutabledictionary *mdict1 = [[Nsmutabledictionary alloc]init];

Set Dictionary: (Add the dictionary with set, array)

Set up

[Mdict1 setobject:@ "Apple" forkey:@ "T1"];

[Mdict1 setobject:@ "Apple" forkey:@ "T2"];

NSLog (@ "%@", mdict1);/*

2015-04-17 14:55:50.335 oclesson5_ Dictionary [1615:87,301] {

T1 = Apple;

T2 = "\u82f9\u679c";

}

*/

To delete a dictionary:

[Mdict1 removeobjectforkey:@ "T1"];

NSLog (@ "%@", mdict1);/*

2015-04-17 14:56:44.512 oclesson5_ Dictionary [1625:87,641] {

T2 = "\u82f9\u679c";

}

*/

To modify a dictionary:

Change

[Mdict1 setobject:@ "banana" forkey:@ "T2"];

NSLog (@ "%@", mdict1);/*

2015-04-17 14:58:59.068 oclesson5_ Dictionary [1639:88,252] {

T2 = Banana;

}

*/

Value:

Check

[Mdict1 objectforkey:@ "T2"];

Empty dictionary:

Empty dictionary

[Mdict1 removeallobjects];

Dictionary class (11)

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.