A dictionary of data sets consisting of key-value pairs
4 Ways to create dictionaries
1> direct assignment for a single key-value pair
1Nsdictionary *dic = [Nsdictionary dictionarywithobject:@"Dajie"Forkey:@"name"];//Create a dictionary directly 2 ID Object= [dic Objectforkey:@"name"];//remove the name key corresponding value 3NSLog (@"%@",Object);
2> creation of a collection with key values
1Nsarray *obj = @[@"Dajie",@"ly"];2Nsarray *keys = @[@"name",@"Address"];3Nsdictionary *dic1 =[Nsdictionary dictionarywithobjects:obj Forkeys:keys]; //Create a dictionary 4NSLog (@"%@", Dic1);
3> Create and assign values directly
1 Nsdictionary *dic2 = [nsdictionary dictionarywithobjectsandkeys:@ "Dajie",@ " name ",@" ly"@"address", Nil];//nil not less 2 NSLog (@ "\ n---%@", Dic2);
3 id obj2 = [dic2 objectforkey:@ "name"];//based on key to remove corresponding value 4 NSLog (@ "%@", obj2);
Commonly used in 4> development
//Xcode is commonly used in the development of new features theNsdictionary *dic3 = @{@"name":@"Dajie",@"Address":@"ly",@"QQ":@"12324124"}; -NSLog (@"%@\n--%@--%ld", DIC3,[DIC3 Objectforkey:@"name"],dic3.count);
Variable dictionaries
2 Nsmutabledictionary *mdic = [Nsmutabledictionary dictionary];//created
Adding elements and deleting elements
//to add a key-value pair -[MDic setobject:@"Dajie"Forkey:@" Person"]; +[MDic setobject:@"Hashiqi"Forkey:@"Dog"]; A[MDic setobject:@"Shapi"Forkey:@"Dog"];//The primary key is the same as the dictionary element, which is overwritten atNSString *str = mdic[@"Dog"]; -NSLog (@"%@", str); - //Delete -[MDic removeallobjects]; -[MDic Removeobjectforkey:@"Dog"]; -NSLog (@"%@", MDic);
Traversing elements
1 Basic for Loop traversal
Nsdictionary *newdic = @{ to @"name":@"Dajie", + @"Dog":@"Hashiqi"
}; -Nsarray *key = [Newdic AllKeys];//Take out all the keys the for(intI i < Newdic.count; i++) { *NSString *keys =Key[i]; $NSString *value =Newdic[keys];Panax NotoginsengNSLog (@"%@ = %@", keys,value); -}
2 Simple traversal
// It is recommended to use fast traversal + enumeratekeysandobjectsusingblock: ^ (ID-IDBOOL *stop) { NSLog (@ "", key, obj); ];
OC Learning Notes Foundation Collection Class Dictionary dictionary