Common nsdictionary Methods
Nsdictionary is used to save data with ing relationships. Therefore, the nsdictionary set stores two sets of values:
A group of keys used to save the nsdictionary
Another group is used to save the value in nsdictionary.
Key and value can be any data of reference type, and one-to-one relationship exists between key and value. (Duplicate keys are not allowed)
1. Create a dictionary object;
Nsdictionary * dicyaoge = [nsdictionary dictionarywithobjectsandkeys: @ "Yao ge", @ "name ",@"? ", @" Sex ", @" 18 ", @" Age ", @" 105 ", @" weight ", nil]; nslog (@" % @", dicyaoge );
Note: This statement is more anti-virus. value is in the front and key is in the back.
Running result:
20:47:06. 329 oc07 _ dictionary [6837: 303] {
Age = 18;
Name = "\ u7476 \ u54e5 ";
Sex = "? ";
Weight = 105;
}
2. Get all key values
NSLog(@"%@", [dicYaoGe allKeys]);
Running result:
20:47:06. 473 oc07 _ dictionary [6837: 303] (
Age,
Sex,
Weight,
Name
)
3. Get all value values
NSLog(@"%@", [dicYaoGe allValues]);
Running result:
20:47:06. 474 oc07 _ dictionary [6837: 303] (
18,
"? ",
105,
"\ U7476 \ u54e5"
)
4. query value by key value
<span style="white-space:pre"></span>NSLog(@"%@",[dicYaoGe objectForKey:@"weight"]);
Running result:
20:47:06. 474 oc07 _ dictionary [6837: 303] 105
Quick traversal:
for (NSString *key in dicYaoGe) { NSLog(@"%@ - %@",key ,[dicYaoGe objectForKey:key]); }
Running result:
20:47:06. 475 oc07 _ dictionary [6837: 303] age-18
20:47:06. 476 oc07 _ dictionary [6837: 303] Sex -?
20:47:06. 476 oc07 _ dictionary [6837: 303] weight-105
20:47:06. 477 oc07 _ dictionary [6837: 303] Name-yao Ge
Common nsmutabledictionary Methods
Nsmutabledictionary * diczijie = [nsmutabledictionary dictionarywithobjectsandkeys: @ "", @ "name", @ "difficult to judge", @ "sex", @ "", @ "Age", @ "200", @ "weight", nil]; for (nsstring * key in diczijie) {nslog (@ "% @-% @", key, [diczijie objectforkey: Key]);}
Running result:
20:57:55. 310 oc07 _ dictionary [6858: 303] age-
20:57:55. 311 oc07 _ dictionary [6858: 303] Hoby -- male
20:57:55. 311 oc07 _ dictionary [6858: 303] weight -- 200
20:57:55. 312 oc07 _ dictionary [6858: 303] Name -- Wang zijie
1. Add a key-value pair.
<span style="white-space:pre"></span>[dicZijie setValue:@"LOL" forKey:@"hoby"];
2. Modify the value corresponding to the key
<Span style = "white-space: pre"> </span> [diczijie setvalue: @ "male" forkey: @ "Hoby"];
3. delete a key-value pair.
<span style="white-space:pre"></span>[dicZijie removeObjectForKey:@"sex"];
4. traverse all key-value pairs through the For Loop
<span style="white-space:pre"></span>for (NSString *key in dicZijie) { NSLog(@"%@-%@",key , [dicZijie objectForKey:key]); }
Running result:
21:00:49. 198 oc07 _ dictionary [6872: 303] age-
21:00:49. 199 oc07 _ dictionary [6872: 303] Hoby-male
21:00:49. 199 oc07 _ dictionary [6872: 303] weight-200
21:00:49. 200 oc07 _ dictionary [6872: 303] Name-Wang zijie
For dictionary traversal in an array:
<Span style = "white-space: pre"> </span> nsdictionary * dicdage = [nsdictionary dictionarywithobjectsandkeys: @ "", @ "name", @ "", @ "Hoby", @ "male", @ "sex", nil]; nsmutablearray * Tongji = [nsmutablearray arraywithobjects: diczijie, dicyaoge, dicdage, nil]; for (nsdictionary * DIC in Tongji) {nslog (@ "______________"); For (nsstring * key in DIC) {nslog (@ "% @ -- % @", key, [DIC objectforkey: Key]) ;}}
Running result:
21:00:49. 201 oc07 _ dictionary [6872: 303] ______________
21:00:49. 201 oc07 _ dictionary [6872: 303] age-
21:00:49. 201 oc07 _ dictionary [6872: 303] Hoby -- male
21:00:49. 202 oc07 _ dictionary [6872: 303] weight -- 200
21:00:49. 202 oc07 _ dictionary [6872: 303] Name -- Wang zijie
21:00:49. 203 oc07 _ dictionary [6872: 303] ______________
21:00:49. 203 oc07 _ dictionary [6872: 303] age -- 18
21:00:49. 204 oc07 _ dictionary [6872: 303] sex --?
21:00:49. 204 oc07 _ dictionary [6872: 303] weight -- 105
21:00:49. 205 oc07 _ dictionary [6872: 303] Name -- Yao Ge
21:00:49. 205 oc07 _ dictionary [6872: 303] ______________
21:00:49. 206 oc07 _ dictionary [6872: 303] Name -- Eldest Brother
21:00:49. 206 oc07 _ dictionary [6872: 303] Hoby -- selling medicine
21:00:49. 207 oc07 _ dictionary [6872: 303] Sex-male
OC's Seventh-Day dictionary lesson7