In OC, the Foundation framework has dictionary classes, dictionary-immutable dictionaries (nsdictionary), and variable dictionaries (nsmutabledictionary), which are used as follows:
Immutable dictionaries:
1 //------------------immutable Dictionary------------------2 3 //Create a Dictionary object, and print the output4Nsdictionary *dic = [Nsdictionary dictionarywithobject:@"Onevalue"Forkey:@"Firstkey"];5NSLog (@"%@", DIC);6 7 //Create a Dictionary object and print the output8Nsdictionary *DICC = [Nsdictionary Dictionarywithobjectsandkeys:@"Onevalue",@"Firstkey",@"Twovalue",@"Secondkey",@"Threevalue",@"Thirdkey",@"Threevalue",@"Forthkey", nil];9NSLog (@"%@", DICC);Ten One //outputs a value object corresponding to a key ANSLog (@"%@", [DICC Objectforkey:@"Firstkey"]); - - //calculates the number of elements in a dictionary (a key-value pair is an element), and outputs a value corresponding to a key theNSLog (@"%lu,%@", [DICC Count], [DICC Objectforkey:@"Secondkey"]); - - - //use arrays to hold keys with the same value +Nsarray * Arraylastout = [DICC allkeysforobject:@"Threevalue"]; -NSLog (@"%@", arraylastout);
Variable dictionaries:
1 //------------------Variable Dictionary------------------2 //Create an empty mutable dictionary3Nsmutabledictionary *mdic = [[Nsmutabledictionary alloc]initwithcapacity:Ten];4 5 //Create a mutable dictionary and assign an initial value6Nsmutabledictionary * MDICC = [Nsmutabledictionary Dictionarywithobjectsandkeys:@"Onevalue",@"Firstkey",@"Twovalue",@"Secondkey",@"Threevalue",@"Thirdkey",@"Threevalue",@"Forthkey", nil];7 8 //assign the full value of a dictionary to another dictionary9 [MDic ADDENTRIESFROMDICTIONARY:MDICC];Ten OneNSLog (@"%@", mDic); A - //use set to update the value of a key in the dictionary, or update the value if key exists -[MDic SetValue:@"Changeonevalue"Forkey:@"Firstkey"]; theNSLog (@"%@", mDic); - - //if the key value does not exist, a pair of Key-value key-value pairs is added -[MDic SetValue:@"NewValue"Forkey:@"Fifthkey"]; +NSLog (@"%@", mDic); - + //completely replaced with the following dictionary contents A [MDICC setdictionary:dic]; atNSLog (@"%@", MDICC); - - - //Delete the contents of a dictionary-delete 1 -[MDICC Removeobjectforkey:@"Firstkey"]; - in //Delete multiple -[MDICC removeobjectsforkeys:@[@"Firstkey",@"Secondkey"]]; to + //Delete all content - [MDICC removeallobjects]; theNSLog (@"%@", MDICC);
Dictionary traversal:
1 //Dictionary Traversal2 //Dictionary Traversal Method 13 for(intx=0; x<mdicc.count; X + +) {4NSLog (@"%@", [MDICC OBJECTFORKEY:[[MDICC AllKeys] objectatindex:x]]);5 }6 7 //Dictionary Traversal Method 28 for(intx=0; x<mdicc.count; X + +) {9NSLog (@"%@", [[MDICC allvalues] objectatindex:x]);Ten } One A //Dictionary Traversal Method 3 - for(IDKeyinchMDICC) { -NSLog (@"%@", [MDICC Objectforkey:key]); the } - - //Dictionary Traversal Method 4 - IDdicx; +Nsenumerator *dicenu =[MDICC Keyenumerator]; - while(dicx=[Dicenu Nextobject]) { +NSLog (@"%@", [MDICC objectforkey:dicx]); A } at - //Dictionary Traversal Method 5 - IDDicenu; -Nsenumerator * Dicenum =[MDICC Objectenumerator]; - while(dicenu=[Dicenum Nextobject]) { -NSLog (@"ddd%@", Dicenu); in}
iOS Development-objc-nsdictionary