In the Cocoa Foundation, nsdictionary and Nsmutabledictionary are used for an ordered collection of objects, Nsdictionary and Nsmutabledictionary and Nsarray and Nsmutablearray are not the same, the former can hold several different types of data, while the latter only holds the same type of data.
1.NSDictionary creation
Nsdictionary *dictionary = [nsdictionary dictionarywithobjectsandkeys:@ "Superdo", @ "Teamname", @ "SuperDo.Horse", @ " Teammember ", nil];
2.NSDictionary traversal
Nsdictionary *dictionary = [nsdictionary dictionarywithobjectsandkeys:@ "Superdo", @ "Teamname", @ "SuperDo.Horse", @ " Teammember ", nil];//gets the number of dictionaries int objectcount = (int) [Dictionary count]; NSLog (@ "Dictionary key number is:%d", objectcount);//Gets all key values in the dictionary nsenumerator * enumeratorkey = [Dictionary keyenumerator];// The fast enumeration iterates through the values of all keys for (NSObject *object in Enumeratorkey) { NSLog (@ "Key value---":%@ ", object);} Gets all value values in the dictionary nsenumerator * enumeratorvalue = [Dictionary objectenumerator];//Fast enumeration iterates over all value values for (NSObject *object In Enumeratorvalue) { NSLog (@ "Value---":%@ ", object);}
3.NSDictionary value found by key
Nsdictionary *dictionary = [nsdictionary dictionarywithobjectsandkeys:@ "Superdo", @ "Teamname", @ "SuperDo.Horse", @ " Teammember ", nil]; NSObject *object = [Dictionary objectforkey:@ "Teamname"];if (Object! = nil) { NSLog (@ "value found through key is:%@", object);}
4.NSMutableDictionary creation
Nsmutabledictionary *dictionary = [Nsmutabledictionary dictionarywithcapacity:10];
5.NSMutableDictionary element operation
Nsmutabledictionary *dictionary = [Nsmutabledictionary dictionarywithcapacity:10]; Dynamically add data to a dictionary [dictionary setobject:@ "Superdo" forkey:@ "Teamname"]; [Dictionary setobject:@ "Superdo.horse" forkey:@ "Teammember"];//through key to find valuensobject *object = [Dictionary objectforkey:@ "Teamname"];//get all Keynsarray *allkeys = [Dictionary allkeys];//get all valuensarray *allvalues = [ Dictionary allvalues];
This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )
Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4594337.html
[Objective-c] 009_foundation framework of Nsdictionary and Nsmutabledictionary