Dictionaries are equivalent to maps in C + + STL
Dictionary Nsdictionary
1 #import <UIKit / UIKit.h>
2 #import "AppDelegate.h"
3
4 int main (int argc, char * argv []) {
5 // Create a dictionary from the @ symbol is actually a key-value pair container
6 NSDictionary * dict = @ {@ "name": @ "zhangsan", @ "sex": @ "male"};
7 NSLog (@ "% @", dict);
8
9 // get value by key
10 NSLog (@ "% @", [dict objectForKey: @ "name"]);
11
12 // Read from the configuration file
13 NSDictionary * dict1 = [NSDictionary dictionaryWithContentsOfFile:
14 [[NSBundle mainBundle] pathForResource: @ "data" ofType: @ "plist"]];
15 NSLog (@ "% @", [dict1 objectForKey: @ "name"]);
16 NSLog (@ "% @", [dict1 objectForKey: @ "age"]);
17
18 @autoreleasepool {
19 return UIApplicationMain (argc, argv, nil, NSStringFromClass ([AppDelegate class]));
20}
twenty one }
Variable dictionary nsmutabledictionary
1 NSMutableDictionary * dict2 = [[NSMutableDictionary alloc] init];
2 // setObject can add a key or modify the value of an existing key
3 [dict2 setObject: @ "jinpangpang" forKey: @ "name"];
4 [dict2 setObject: @ "113" forKey: @ "name"];
5 NSLog (@ "% @", [dict2 objectForKey: @ "name"]);
Objective-c Dictionary, Variable Dictionary