一、NSDictionary的使用
1 int main(int argc, const char * argv[])
2 {
3
4 @autoreleasepool {
5
6 // 创建方式一
7 NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:
8 @"value1", @"key1",
9 @"value2", @"key2", nil];
10 NSLog(@"%@", dict1);
11 // 创建方式二
12 NSDictionary *dict2 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"value1", @"value2", nil] forKeys:[NSArray arrayWithObjects:@"key1", @"key2", nil]];
13 NSLog(@"%@", dict2);
14
15 // 获取键值对个数
16 NSLog(@"%zi", [dict2 count]);
17 // 根据key获取value
18 NSLog(@"%@", [dict2 objectForKey:@"key1"]);
19
20 // 遍历所有key
21 for (id key in dict2) {
22 NSLog(@"%@", [dict2 objectForKey:key]);
23 }
24
25
26 }
27 return 0;
28 }