Nsdictionary
Immutable dictionaries
Ways to create a dictionary
How to create a dictionary nsdictionary *dy = [Nsdictionary dictionarywithobject:@ "a" forkey:@ "B"]; Nsdictionary *dy1 = [Nsdictionary dictionarywithobjectsandkeys:@ "a", @ "B", @ "C", @ "D", nil]; Quick Create dictionary //duplicate key value, duplicate cannot save to dictionary nsdictionary *dy2 = @{@ "we": @ "haha", @ "we": @ "Ha"}; NSLog (@ "%@", dy); NSLog (@ "%@", dy1); NSLog (@ "%@", Dy2);
Get the length of the dictionary
//Get dictionary lengthNsdictionary *dy2 = @{@"We":@"haha",@"W":@"ha"}; NSLog (@"%lu", Dy2.count);//Output 2//if the key value is repeated, the length is not countedNsdictionary *dy2 = @{@"We":@"haha",@"We":@"ha"}; NSLog (@"%lu", Dy2.count);//Output 1
The traversal of a dictionary
Nsdictionary *dy1 = [Nsdictionary Dictionarywithobjectsandkeys:@"a",@"b",@"C",@"D", nil]; //quickly create a dictionary//Duplicate key value, duplicate cannot be saved to dictionaryNsdictionary *dy2 = @{@"We":@"haha",@"W":@"ha"}; NSLog (@"%lu", Dy2.count); //the traversal of a dictionary for(NSString *keyinchdy2) {NSLog (@"key =%@, value =%@", Key, [Dy2 Objectforkey:key]); } //Enumeration Type Traversal[Dy1 enumeratekeysandobjectsusingblock:^ (IDKeyIDobj, BOOL *stop) {NSLog (@"key =%@, value =%@", key, obj); }];
Save the dictionary in a file
//Save the dictionary to a fileNsdictionary *dy2 = @{@"We":@"haha",@"W":@"ha"}; BOOL Iswrite= [Dy2 writetofile:@"/users/cloudwalk/desktop/test.plist"Atomically:yes]; if(iswrite) {NSLog (@"Write Success"); }
Reading a dictionary from a file
Nsdictionary *readdy2 = [nsdictionary dictionarywithcontentsoffile:@ "/users/cloudwalk/desktop/ Test.plist"]; NSLog (@ "%@", readDy2);
Build an array dictionary by adding arrays to the dictionary
Nsarray *sdarr = [Nsarray arraywithobjects:@"Zaozhuang",@"Jinan", nil]; Nsarray*jxarr = [Nsarray arraywithobjects:@"Jiujiang",@"Nanchang", nil]; Nsdictionary*citys = [Nsdictionary Dictionarywithobjectsandkeys:sdarr,@"SD", Jxarr,@"JX", nil]; NSLog (@"citys =%@", citys);
Nsdictionary *citys = [Nsdictionary Dictionarywithobjectsandkeys:sdarr,@ "SD", Jxarr, @ "jx", nil]; NSLog (@ "citys =%@", citys); // Save the Citys in a file [Citys writetofile:@ "/users/cloudwalk/desktop/test1.plist" Atomically:yes];
OBJECTIVE-C (Foundation framework One nsdictionary)