Dictionary
1. A dictionary (dictionary) is a collection of data consisting of key-object pairs. You can get the desired value (that is, the object) from the Objective-c dictionary by the key of the object, just as you would find the word definition in the dictionary. The keys in the dictionary must be single-valued, usually they are strings, but they can also be other object types. The value associated with the key can be any object type, but cannot be nil.
2. Practice Code
//1.dictionary Create an empty dictionaryNsdictionary *dictionary =[Nsdictionary dictionary];//2. Create and initialize//Dictionarywithobject: (ID) Forkey: (ID) nscopying>Nsdictionary *dictionary1 = [Nsdictionary dictionarywithobject:@"IOS"Forkey:@"Android"]; NSLog (@"%@", Dic1);//3. Create and return a dictionary. //Dictionarywithobjects:forkeysNsarray * keys = [Nsarray arraywithobjects:@"name",@"Sex",@" Age", nil]; Nsarray* values = [Nsarray arraywithobjects:@"Apple",@ "Android", [NSNumber Numberwithint: -], nil]; Nsdictionary* Dictionary3 =[Nsdictionary dictionarywithobjects:values Forkeys:keys]; NSLog (@"%@", dictionary3);//4. Use key-object pair {Key1,obj1}, {key2,obj2} ... Create a dictionary//DictionarywithobjectsandkeysNsdictionary * Dictionary4 =[nsdictionary Dictionarywithobjectsandkeys:@"IOS",@"name", @ -,@" Age", nil]; NSLog (@"%@", Dictionary4);//5.AllKeys Returns an array containing all the keys in the dictionaryNSLog (@"allkeys:%@", [Dictionary5 AllKeys]);//6.Allkeysforobject: Returns an array that contains all the keywords that correspond to the given object. Nsarray * AA = @[@"name",@"IOS",@ "WInphone",@"Android"]; NSLog (@"aaaaaaa%@", [Dictionary5 Allkeysforobject:aa]);//7.allvalues: Returns an array containing all the values in the dictionaryNSLog (@"allvalues:%@", [Dictionary5 allvalues]);//8.Objectforkey: Returns the value of the specified key, without which the key returns nil.NSLog (@"%@", [Dictionary5 Objectforkey:@"name"]);//9.The Valueforkey also returns the value of the specified key. NSLog (@"%@", [Dictionary5 Valueforkey:@"name"]);
//10.count: Returns the number of records in the dictionaryNSLog (@"Count:%zi", [dictionary5 Count]);//11.traverse the key in the dictionaryNSLog (@"Traverse dictionary \ n");//Fast Traversal for(IDKeyinchdictionary5) {NSLog (@"%@", key);}//iterate through the value in the dictionary for(IDKeyinchdictionary5) {NSLog (@"%@", [Dictionary5 Valueforkey:key]);}//12.Variable DictionariesNsmutabledictionary * Dictionary2 =[Nsmutabledictionary dictionary];//13.Setobject:forkey: Add value and key and replace the value if key exists[Dictionary2 SetValue:@"Itheima"Forkey:@" First"]; [Dictionary2 SetValue:@"Itcast"Forkey:@"Second"]; NSLog (@"%@", Dictionary2);//14.Removeobjectforkey Delete the record for the specified key in the dictionary[Dic2 Removeobjectforkey:@" -"]; NSLog (@"%@", Dictionary2);//15.removeallobjects Delete all records in a dictionary[Dic2 removeallobjects]; NSLog (@"%@", Dictionary2); Nsdictionary* Diction1 =[nsdictionary Dictionarywithobjectsandkeys:@"IOS",@"Name", @ the,@" Age", nil]; Nsdictionary* Diction2 =[nsdictionary Dictionarywithobjectsandkeys:@"Android",@"Name", @ -,@" Age", nil];//16.isequaltodictionary Comparison of two dictionariesif([Diction1 Isequaltodictionary:diction2]) {NSLog (@"diction1 equal Diction2");}Else{NSLog (@"diction1 Not Equal diction2");}//17.WriteToFile writing a dictionary to a file (in XML format)NSString * Path =@"/users/caile/diction1.xml"; [Diction1 Writetofile:path Atomically:yes];//18.Dictionarywithcontentsoffile read a file back to the new Dictionary objectNsdictionary *newdic =[Nsdictionary Dictionarywithcontentsoffile:path];
Dark Horse programmer _ios Development _objective-c Learning Notes _ Dictionary